0

I am very new to powershell and writing a script which will go to each computer in windows domain and get the size of a user profile. I have tried below on a powershell script

$profileDir = "\\$computerName\c$\users\userProfile"
$fullProfile = (Get-ChildItem $profileDir -recurse -force | Measure-Object -property length -sum)

But on some computer it gives below error. The problem seems that some directories on the profile have a long path and get-ChildItem fails

Get-ChildItem : The specified path, file name, or both are too long. The fully
qualified file name must be less than 260 characters, and the directory name mu
st be less than 248 characters.
At C:\scripts\powershell\profiles.ps1:56 char:30
+ $fullProfile = (Get-ChildItem <<<<  $profileDir -recurse -force | Measure-Obj
ect -property length -sum)
    + CategoryInfo          : ReadError: (\\computerName...nt.IE5\RQT4K4JU:St
   ring) [Get-ChildItem], PathTooLongException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChil
   dItemCommand
##########################################################################################################################

At this point I tried using du.exe (diskusage) from SysInternals which works fine but I don't know how to take the output of du into a variable. I have below on my script

$dirSize = .\du.exe -c c:\temp |convertFrom-csv | select-object DirectorySize
write-host $dirSize

The output is

PS C:\scripts\powershell> .\profiles.ps1

@{DirectorySize=661531}

What I want the output to be like

PS C:\scripts\powershell> .\profiles.ps1

661531
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1212915
  • 31
  • 1
  • 1
  • 4

1 Answers1

1

What you have is a hashtable. You need to reference the item in that hashtable to get it's value. Instead of:

Write-Host $dirSize

Try:

$dirSize['DirectorySize']
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Thanks but I get below error PS C:\scripts\powershell> .\profiles.ps1 Unable to index into an object of type System.Management.Automation.PSObject. At C:\scripts\powershell\profiles.ps1:7 char:21 + write-host $dirSize[ <<<< 'DirectorySize'] + CategoryInfo : InvalidOperation: (DirectorySize:String) [], RuntimeException + FullyQualifiedErrorId : CannotIndex – user1212915 Nov 06 '14 at 03:15
  • OK, try $dirsize.directorysize – TheMadTechnician Nov 06 '14 at 03:19
  • Your hashtable comment gave me a clue. I tried $dirSize.DirectorySize and the output is now as expected. PS C:\scripts\powershell> .\profiles.ps1 661531 Thank you again! – user1212915 Nov 06 '14 at 03:20