Skip to content

Instantly share code, notes, and snippets.

@dpo007
Last active June 29, 2023 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpo007/b2877162908217493eac73b44066c3b8 to your computer and use it in GitHub Desktop.
Save dpo007/b2877162908217493eac73b44066c3b8 to your computer and use it in GitHub Desktop.
PowerShell :: Get-UsersWithMismatchedHomeDirectory
<#
.SYNOPSIS
Retrieves users whose home directory paths do not contain their SamAccountName.
.DESCRIPTION
The Get-UsersWithMismatchedHomeDirectory function retrieves users from Active Directory whose home directory paths do not contain their SamAccountName. By default, it excludes users with empty home directories. Users can be optionally included by specifying the -IncludeEmptyHomeDirs switch parameter.
.PARAMETER IncludeEmptyHomeDirs
Switch parameter to include users with empty home directories in the results.
.EXAMPLE
$usersWithMismatchedHomeDir = Get-UsersWithMismatchedHomeDirectory -IncludeEmptyHomeDirs
$usersWithMismatchedHomeDir | Format-Table Name, HomeDirectory
This example retrieves all users whose home directory paths do not contain their SamAccountName, including users with empty home directories. The resulting collection of users is displayed in a table format, showing the user's name and home directory path.
.NOTES
- Requires the Active Directory module.
- You must be connected to an Active Directory domain controller to use this function.
#>
function Get-UsersWithMismatchedHomeDirectory {
param (
[switch]$IncludeEmptyHomeDirs
)
$users = Get-ADUser -Filter * -Property HomeDirectory
if (-not $IncludeEmptyHomeDirs) {
$users = $users | Where-Object { $_.HomeDirectory -ne $null -and $_.HomeDirectory -ne '' }
}
$mismatchedUsers = $users | Where-Object { $_.HomeDirectory -notlike "*\$($_.SamAccountName)*" }
return $mismatchedUsers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment