Dear community ,

First of all let me take this opportunity to say hi , what a great community you have here. Am a powershell novice and am having problem with my script. Basically , the script is meant to read from a csv file containing a list of Pcs , view the local user profile location ( C:\Users) on each pc and output the data to another csv. So far all my script can do is just output the user profiles of all the computers in the csv file but not the computername so I cannnot tell what userprofiles are from which machine.

Here is my script 

**********************************************************************

$machineinfo = import-csv "D:\machine.csv" -header ("Machine")

ForEach ($item in $machineinfo) {

Write-Host $item.Machine

$item.Machine >> D:\output.csv

Get-ChildItem -Path C:\Users >> D:\output.csv

}

***********************************************************************

Any advice will be highly appreciated. Thanks.

Answer Summary:
Cancel
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Answer Chosen by the Author

4
$machineinfo = import-csv "D:\machine.csv" -header ("Machine")

ForEach ($item in $machineinfo) {
$machine = $item.Machine
Write-Host $machine
if (test-path("\\$machine\C$\Users")) {
$users = (Get-ChildItem \\$machine\C$\Users).Name
$out = [string] ($users -join ";")
Write-Output "$machine,$out" >> D:\output.csv
} else {
Write-Output "$machine,Error reading profiles." >> D:\output.csv
}
}

Answered 02/18/2015 by: flip1001
Fourth Degree Green Belt

Please log in to comment

Community Chosen Answer

2
I'd use a more "Windows-ish" way to get the profiles used on a computer :

$computers = import-csv "D:\machine.csv" -header ("Machine")
$users = @()
$members = @{
        Name = ""
        SID = ""
        Path = ""
}
foreach ($computer in $computers)
    $profiles = gwmi win32_userprofile -ComputerName $computer | select localpath,sid
    foreach ($profile in $profiles) {
        $user = New-Object psobject -Property $members
        $user.SID = New-Object System.Security.Principal.SecurityIdentifier($profile.sid)
        $user.Name = $user.SID.Translate([System.Security.Principal.NTAccount]).Value
        $user.Path = $profile.localpath
        $users += $user
    }
}

$users | Export-Csv -Path "c:\users.csv"


Regards,

Math
Answered 06/02/2015 by: mbouju
White Belt

Please log in to comment

Answers

0
Thanks Flip1001! I owe you :-)
Answered 02/19/2015 by: TonyFishers
Senior White Belt

Please log in to comment
-1
Thanks Flip1001! I owe you :-)
Answered 02/19/2015 by: TonyFishers
Senior White Belt

Please log in to comment
Answer this question or Comment on this question for clarity