The task seems trivial! A lot of users come and go in our environment. Every now and then I want to check for "orphan" roaming profile folders and get rid of them. My code works if the user name matches the folder name exactly but not for folders where Windows has added a .V2, .V3 etc. E.g. -eq works but not -like or -contains, or any of the ways I've tried to insert a wildcard. I tried a where-object construction and failed as well.
For example, if we have a user bob.smith, I want don't want my "orphan" list to include profile folders named bob.smith or bob.smith.V2.
Here's my code:
# Put profile folder names in array
$profilefolders = Get-ChildItem -Name "\\server\profiles$"
# Initialize arrays
$orphanprofiles = @()
foreach ($userprofile in $profilefolders)
{
# Do profile names match up with user accounts?
If(Get-ADUser -Filter {SamAccountName -eq $userprofile})
{
# User account exists: no action
}
Else
{
# Profile is an orphan. Add to list.
$orphanprofiles += $userprofile
}
}
# put array values in a string variable with newline char for screen output
$outputprofiles = $orphanprofiles -join "`n"
Write-Output "`n"
Write-Output "Orphan profiles: `n$outputprofiles"
Write-Output " "
Change the first line to :
Came up with a better solution. It uses a RegEx to split the
.V2
extension from the foldername and so checks the name without that ext. The intermediate $profilefolders isn't required.