/build/static/layout/Breadcrumb_cap_w.png

Scripting Question


Powershell- Help with script to display\Output local user profiles on multiple machines

02/17/2015 4467 views
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:
0 Comments   [ + ] Show comments

Comments


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
Fifth Degree Brown Belt


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

All Answers

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

-1
Thanks Flip1001! I owe you :-)
Answered 02/19/2015 by: TonyFishers
Yellow Belt

 
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