I'm trying to create a PowerShell script that retrieves the home directories of all users on my system, but it seems to only return the directory of the currently logged-in user. Here's what I've got so far:
```powershell
$startFolder = "inpromoepfsusers"
$output = "c:TempFilename_Userlist_size.csv"
$colItems = (Get-ChildItem $startFolder | Where-Object { $_.PSIsContainer -eq $True } | Sort-Object)
$results = [System.Collections.ArrayList] @()
foreach ($i in $colItems) {
$row = [PSCustomObject]@{
'Directory(Sched Col)' = $i.FullName
'User' = $i.Name
'Size in MB' = [Double][math]::round(((Get-ChildItem $i.FullName -Recurse | Measure-Object length -sum).sum) / 1MB, 2)
'OneDrive (Sched Col)' = 'https://doimspp-my.sharepoint.com/personal/' + $i.Name + '_nps_gov'
'Documents (Sched Col)' = 'Documents'
'Home Drive(Sched Col)' = 'Home_Drive'
}
$results.Add($row)
}
$results | Export-Csv $output -NoTypeInformation
```
Can anyone help me troubleshoot this?
4 Answers
Quick tip on code formatting for the next time: when you paste code here, use a PowerShell editor to indent it, or you can manually format it with spaces for better visibility. It helps others read your code easily!
Here’s a revision that worked for me:
```powershell
$startFolder = 'c:users'
$output = "c:TempFilename_Userlist_size.csv"
$colItems = Get-ChildItem $startFolder -Directory -ErrorAction SilentlyContinue | Sort-Object
$results = foreach ($i in $colItems) {
$fileSum = $i | Get-ChildItem -Recurse -File | Measure-Object length -sum
[PSCustomObject]@{
'Directory(Sched Col)' = $i.FullName
'User' = $i.Name
'Size in MB' = [Double][math]::round($fileSum.sum / 1MB, 2)
'OneDrive (Sched Col)' = 'https://doimspp-my.sharepoint.com/personal/{0}_nps_gov' -f $i.Name
'Documents (Sched Col)' = 'Documents'
'Home Drive(Sched Col)' = 'Home_Drive'
}
}
$results | Export-Csv $output -NoTypeInformation
```
Make sure the start folder is correctly set!
Are you sure your account has the necessary permissions? As an admin, you should ideally have read access to other users' folders. If that’s all good, try validating your `$startFolder` variable. You can also simplify your code to use `-Directory` and `-File` options directly when calling `Get-ChildItem`, which can help streamline things a bit!
It looks like the issue may stem from how you’re using the path or the permissions of the user running the script. You should check if you have access to all the user directories. If you're using a UNC path, ensure that any backslashes are properly escaped or consider using single quotes for the path. Instead of filtering for containers with `Where-Object`, you can use the `-Directory` parameter directly with `Get-ChildItem`. It makes the code cleaner!

Yeah, I’m using my admin account, so permissions shouldn’t be the problem. I'll double-check the path!