Last active
June 29, 2023 19:02
-
-
Save dpo007/b2877162908217493eac73b44066c3b8 to your computer and use it in GitHub Desktop.
PowerShell :: Get-UsersWithMismatchedHomeDirectory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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