I'm trying to write a PowerShell script that pulls the number of files of each users home directory in Active Directory. I've come up with the following script but it's not actually getting the number of files, my file count is 0 for each user. What did I miss in the Expression? I tried % in place of ? and I tried adding ! in front of $_ with incorrect results.
Get-ADUser -Filter * -properties * -SearchBase "OU=Information Technology,`
OU=User Accounts,DC=net,DC=local" | ft name, homedirectory, homedrive,`
@{Name='Files'; Expression={(Get-ChildItem -Recurse -Force -ErrorAction Ignore`
| ?{$_.HomeDirectory}).count}} -A
As randoms points out, you'll need to supply $_.HomeDirectory
as an argument to Get-ChildItem
.
To avoid running Get-ChildItem
when a HomeDirectory
attribute is empty or doesn't exist, you could put an if
statement in the expression (split into multiple statements for readability here):
$ITUsers = Get-ADUser -Filter * -properties homedirectory,homedrive -SearchBase "OU=Information Technology,OU=User Accounts,DC=net,DC=local"
$ITUsers |Format-Table name, homedirectory, homedrive,@{Name='Files'; Expression={
if(Test-Path $_.homedirectory){
@(Get-ChildItem $_.homedirectory -Recurse -Force -ErrorAction Ignore).Count
}
else {
0
}
} -AutoSize