Thursday, May 3, 2018

User Home Drive Report


I was tasked with creating a report on our user home drives of information on every file, such as name, size, modified date, file extension, etc. They also wanted to include information on the user, including things like their name, location, department, and title. Since the home drives are created with the username, it made it pretty easy.

Get-ChildItem "\\fileserver\users$" |Where-Object {$_.psiscontainer -eq $true}|Select-Object -First 10|ForEach-Object{
 Get-ChildItem $_.FullName -Recurse -ErrorAction SilentlyContinue|Where-Object {$_.psiscontainer -eq $false}|Foreach-Object{
  $EID = $_.FullName
  $EID = $EID.Trim("\\\\fileserver\\users$\\")
  $EID = $EID.Substring(0,6)
  $User = (get-aduser $EID -Properties GivenName,Surname,Title,StreetAddress,Department,HomeDirectory)
  $UName = $User.GivenName + " " + $User.Surname
  $Server=$_.FullName.Trim("\\\\")
  $Server=$Server.Split('\\')[0]
 
$props = @{
    EmployeeID = $EID
    UserName = $UName
    UserTitle = $User.Title
    UserLocation = ($User.StreetAddress|Select-Object -First 1)
    UserDepartment = $User.Department
    UserHomeDirectory = $User.HomeDirectory
    File = $_.FullName
    FileServer = $Server
    Extension = $_.Extension
    CreationTime = $_.CreationTime
    LastWriteTime = $_.LastWriteTime
    Name = $_.Name
    SizeInKB = [math]::Round($_.Length/1024,2)
}
  $Export = new-object psobject -Property $props 
  $Export|export-csv -append C:\temp\First10.csv -NoTypeInformation
  $User = $null
}
}

Notes: This was made with the assumption that the employeeID is a fixed length of 6 characters, so you may need to adjust for your environment. Also, if you have a large number of users or files, you will need to break the data into more manageable sizes. I did this using the Select-Object command.