I was able to run this on my local machine and output to a csv file successfully:

$Name = hostname

gwmi win32_userprofile | select @{LABEL= ”PC”;EXPRESSION={$Name}}, @{LABEL= ”Last Used”;EXPRESSION={$.ConvertToDateTime($.lastusetime)}},LocalPath, SID |sort-object -property $Name | Out-File C:\test1.csv

I want to get this same thing for each computer listed in a csv file, I have the below which runs with no errors but doesn’t write anything to the csv file.

$Name = hostname

$computers = get-content “G:\IS\Level1\IT Procedures and Documentation__IS Manuals\Network Manual\Section 20 - Help Desk\Test PC\PClist - Copy.csv”

foreach ($computer in $computers) {gwmi win32_userprofile -Computername $computers | select @{LABEL= ”PC”;EXPRESSION={$Name}}, @{LABEL= ”Last Used”;EXPRESSION={$.ConvertToDateTime($.lastusetime)}},LocalPath, SID | Ft -autosize | export-csv C:\Users\malena\Desktop\test1.csv}

Any ideas on what I am doing wrong?

5 Spice ups

The problem is that GWMI returns an array of objects, not just one.

1 Spice up

I am pretty new at making scripts, can anyone tell me how to fix this?

Apart from what Thomas just said, there are some issues:

$Name = hostname

$computerList = get-content "G:\IS\Level1\IT Procedures and Documentation\__IS Manuals\Network Manual\Section 20 - Help Desk\Test PC\PClist - Copy.csv"

foreach ($computer in $computerList) {
    gwmi win32_userprofile -Computername $computer | 
        select @{n="PC";e={$Name}}, @{n="Last Used";e={$_.ConvertToDateTime($_.lastusetime)}}, LocalPath, SID | 
        export-csv C:\Users\malena\Desktop\test1.csv
}

inside the loop you should be querying the $computer variable in gwmi.
Deleted the use of Format-table. You’re exporting to csv, using FT will give you garbage in the file.
You are also setting ‘PC’ to be $name every time. Did you mean to use $computer instead? Or something else?

1 Spice up

Thank you so much, that worked well except now I have to resolve another issue. The script only returns data for the last pc listed in the csv file.

1 Spice up

make the line:

export-csv C:\Users\malena\Desktop\test1.csv -append

3 Spice ups

thank you, my issue is now resolved.

$computerList = get-content “G:\IS\Level1\IT Procedures and Documentation__IS Manuals\Network Manual\Section 20 - Help Desk\Test PC\PClist - Copy.txt”

foreach ($computer in $computerList) {

gwmi win32_userprofile -Computername $computer |

select pscomputername, @{n=“Last Used”;e={$.ConvertToDateTime($.lastusetime)}}, LocalPath, SID |

export-csv C:\Users\malena\Desktop\test1.csv -NoTypeInformation -Append

}

1 Spice up

If your issue is resolved, please mark a “Best Answer,” and if appropriate, a “Helpful Answer.” That shows the thread has been answered

1 Spice up