/build/static/layout/Breadcrumb_cap_w.png

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:
Posted by: flip1001 9 years ago
Black Belt
4

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
White Belt
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
Posted by: TonyFishers 9 years ago
Yellow Belt
0
Thanks Flip1001! I owe you :-)
Posted by: TonyFishers 9 years ago
Yellow Belt
-1
Thanks Flip1001! I owe you :-)

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ