I was able to run this on my local machine and output to a csv file successfully:<\/p>\n
$Name = hostname<\/p>\n
gwmi win32_userprofile | select @{LABEL= ”PC”;EXPRESSION={$Name}}, @{LABEL= ”Last Used”;EXPRESSION={$.ConvertToDateTime($<\/em>.lastusetime)}},LocalPath, SID |sort-object -property $Name | Out-File C:\\test1.csv<\/p>\n 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.<\/p>\n $Name = hostname<\/p>\n $computers = get-content “G:\\IS\\Level1\\IT Procedures and Documentation__IS Manuals\\Network Manual\\Section 20 - Help Desk\\Test PC\\PClist - Copy.csv”<\/p>\n foreach ($computer in $computers) {gwmi win32_userprofile -Computername $computers | select @{LABEL= ”PC”;EXPRESSION={$Name}}, @{LABEL= ”Last Used”;EXPRESSION={$.ConvertToDateTime($<\/em>.lastusetime)}},LocalPath, SID | Ft -autosize | export-csv C:\\Users\\malena\\Desktop\\test1.csv}<\/p>\n Any ideas on what I am doing wrong?<\/p>","upvoteCount":5,"answerCount":8,"datePublished":"2016-03-18T16:21:31.000Z","author":{"@type":"Person","name":"malenaf","url":"https://community.spiceworks.com/u/malenaf"},"acceptedAnswer":{"@type":"Answer","text":" Apart from what Thomas just said, there are some issues:<\/p>\n inside the loop you should be querying the $computer variable in gwmi. I was able to run this on my local machine and output to a csv file successfully:<\/p>\n $Name = hostname<\/p>\n gwmi win32_userprofile | select @{LABEL= ”PC”;EXPRESSION={$Name}}, @{LABEL= ”Last Used”;EXPRESSION={$.ConvertToDateTime($<\/em>.lastusetime)}},LocalPath, SID |sort-object -property $Name | Out-File C:\\test1.csv<\/p>\n 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.<\/p>\n $Name = hostname<\/p>\n $computers = get-content “G:\\IS\\Level1\\IT Procedures and Documentation__IS Manuals\\Network Manual\\Section 20 - Help Desk\\Test PC\\PClist - Copy.csv”<\/p>\n foreach ($computer in $computers) {gwmi win32_userprofile -Computername $computers | select @{LABEL= ”PC”;EXPRESSION={$Name}}, @{LABEL= ”Last Used”;EXPRESSION={$.ConvertToDateTime($<\/em>.lastusetime)}},LocalPath, SID | Ft -autosize | export-csv C:\\Users\\malena\\Desktop\\test1.csv}<\/p>\n Any ideas on what I am doing wrong?<\/p>","upvoteCount":5,"datePublished":"2016-03-18T16:21:31.000Z","url":"https://community.spiceworks.com/t/powershell-gwmi-win32_userprofile/482061/1","author":{"@type":"Person","name":"malenaf","url":"https://community.spiceworks.com/u/malenaf"}},{"@type":"Answer","text":" The problem is that GWMI returns an array of objects, not just one.<\/p>","upvoteCount":1,"datePublished":"2016-03-18T16:29:14.000Z","url":"https://community.spiceworks.com/t/powershell-gwmi-win32_userprofile/482061/2","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":" I am pretty new at making scripts, can anyone tell me how to fix this?<\/p>","upvoteCount":0,"datePublished":"2016-03-18T16:37:48.000Z","url":"https://community.spiceworks.com/t/powershell-gwmi-win32_userprofile/482061/3","author":{"@type":"Person","name":"malenaf","url":"https://community.spiceworks.com/u/malenaf"}},{"@type":"Answer","text":" 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.<\/p>","upvoteCount":1,"datePublished":"2016-03-18T17:01:34.000Z","url":"https://community.spiceworks.com/t/powershell-gwmi-win32_userprofile/482061/5","author":{"@type":"Person","name":"malenaf","url":"https://community.spiceworks.com/u/malenaf"}},{"@type":"Answer","text":"$Name = hostname\n\n$computerList = get-content \"G:\\IS\\Level1\\IT Procedures and Documentation\\__IS Manuals\\Network Manual\\Section 20 - Help Desk\\Test PC\\PClist - Copy.csv\"\n\nforeach ($computer in $computerList) {\n gwmi win32_userprofile -Computername $computer | \n select @{n=\"PC\";e={$Name}}, @{n=\"Last Used\";e={$_.ConvertToDateTime($_.lastusetime)}}, LocalPath, SID | \n export-csv C:\\Users\\malena\\Desktop\\test1.csv\n}\n\n<\/code><\/pre>\n
\nDeleted the use of Format-table. You’re exporting to csv, using FT will give you garbage in the file.
\nYou are also setting ‘PC’ to be $name every time. Did you mean to use $computer instead? Or something else?<\/p>","upvoteCount":1,"datePublished":"2016-03-18T16:38:50.000Z","url":"https://community.spiceworks.com/t/powershell-gwmi-win32_userprofile/482061/4","author":{"@type":"Person","name":"psophos","url":"https://community.spiceworks.com/u/psophos"}},"suggestedAnswer":[{"@type":"Answer","text":"