Skip to content

Instantly share code, notes, and snippets.

@h-otter
Last active August 29, 2015 14:25
Show Gist options
  • Save h-otter/4557d2f8de073e81c6a7 to your computer and use it in GitHub Desktop.
Save h-otter/4557d2f8de073e81c6a7 to your computer and use it in GitHub Desktop.
# Set domain
$usersDomain = ""
# Set ad server
$ADServerName = ""
# Set forced directory
$forcedDirectory = ""
function makeEnableUsersDirectory{
[CmdletBinding()]
param(
[parameter(
position = 0,
mandatory = 0
)]
[string]
$path,
$domain,
$ADServer
)
$enableUsers = Get-ADUser -Server $ADServer -Filter {Enabled -eq $true} | select SamAccountName # Getting Enable Users
for ($i = 0; $i -lt $enableUsers.length; $i++)
{
$newDirectory = Join-Path $path $enableUsers[$i].SamAccountName
New-Item $newDirectory -ItemType directory # Make New Directory
if (Test-Path -Path $newDirectory)
{
$user = $enableUsers[$i].SamAccountName + '@' + $domain
icacls $newDirectory /setowner $user # Set Owner
}
else
{
Write-Warning "File not exist. Please check path you tried."
}
}
icacls $path /reset /t
}
function deleteDisableUsersDirectory{
[CmdletBinding()]
param(
[parameter(
position = 0,
mandatory = 0
)]
[string]
$path
)
$disableUsers = Get-ADUser -Filter {Enabled -eq $false} | select SamAccountName # Getting Disable Users
for ($i = 0; $i -lt $enableUsers.length; $i++)
{
$oldDirectory = Join-Path $path $disableUsers[$i].SamAccountName
Remove-Item $oldDirectory
}
}
makeEnableUsersDirectory -path $forcedDirectory -domain $usersDomain -ADServer $ADServerName
deleteDisableUsersDirectory -path $forcedDirectory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment