Powershell- Help with script to display\Output local user profiles on multiple machines
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.
0 Comments
[ + ] Show comments
Answers (4)
Answer Summary:
Please log in to answer
Posted by:
flip1001
9 years ago
Top Answer
$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
}
}
Posted by:
mbouju
9 years ago
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