Scripting Question

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

02/17/2015 4807 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


Answer Chosen by the Author

$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
Black Belt

Community Chosen Answer

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"


Answered 06/02/2015 by: mbouju
White Belt

All Answers

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

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

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:


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