Created
June 29, 2023 18:59
-
-
Save dpo007/71da7651d836a437bed1fbfed127aa23 to your computer and use it in GitHub Desktop.
PowerShell :: Find-NonMatchingADUserFolders
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 | |
Script to find subfolders without matching Active Directory (AD) users and optionally move them to a destination folder. | |
.DESCRIPTION | |
This PowerShell script imports the Active Directory module and defines a function named Find-NonMatchingADUserFolders. The function takes two parameters: folderPath (mandatory) and destinationFolder (optional). It retrieves all subfolders in the specified folder and all existing AD users. It then iterates through each subfolder, checks if the subfolder name matches an existing AD user, and performs the specified action (move or list) based on the presence or absence of a matching user. | |
.EXAMPLE | |
Find-NonMatchingADUserFolders -folderPath "C:\Path\To\Folder" -destinationFolder "D:\Temp\OldUserFolders" | |
Moves the subfolders without matching AD users to the specified destination folder. | |
.EXAMPLE | |
Find-NonMatchingADUserFolders -folderPath "C:\Path\To\Folder" | |
Lists the subfolders without matching AD users without moving them. | |
#> | |
# Import the Active Directory module | |
Import-Module ActiveDirectory | |
function Find-NonMatchingADUserFolders { | |
param( | |
[Parameter(Mandatory = $true)] | |
[string]$folderPath, | |
[string]$destinationFolder | |
) | |
# Get all subfolders in the specified folder | |
$subfolders = Get-ChildItem -Path $folderPath -Directory | |
# Get all existing AD users | |
$adUsers = Get-ADUser -Filter * | |
# If destination folder is provided, create it if it doesn't exist | |
if ($destinationFolder) { | |
if (-not (Test-Path -Path $destinationFolder)) { | |
New-Item -Path $destinationFolder -ItemType Directory | Out-Null | |
} | |
} | |
# Iterate through each subfolder | |
foreach ($subfolder in $subfolders) { | |
# Check if the subfolder name matches an existing AD user | |
$matchingUser = $adUsers | Where-Object { $_.SamAccountName -eq $subfolder.Name } | |
# If there is no matching AD user | |
if ($matchingUser -eq $null) { | |
# If destination folder is provided, move the subfolder | |
if ($destinationFolder) { | |
Move-Item -Path $subfolder.FullName -Destination $destinationFolder | |
Write-Host "Moved subfolder without matching AD user: $($subfolder.FullName)" | |
} | |
else { | |
# If no destination folder is provided, list the subfolder | |
Write-Host "Subfolder without matching AD user: $($subfolder.FullName)" | |
} | |
} | |
} | |
} | |
# Usage examples: | |
# Example 1: Move the subfolders without matching AD users to a destination folder | |
Find-NonMatchingADUserFolders -folderPath "C:\Path\To\Folder" -destinationFolder "D:\Temp\OldUserFolders" | |
# Example 2: List the subfolders without matching AD users without moving them | |
Find-NonMatchingADUserFolders -folderPath "C:\Path\To\Folder" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment