Forum Discussion
TJCooper440
Jan 28, 2025Copper Contributor
I cant get this syntax correct
I have accounts with the prefix AB that correspond to normal accounts. For example, AB-Test and Test. I want to query an OU for all the prefixed accounts, strip the AB-, and then lookup the resultant...
Andres-Bohren
Jan 28, 2025Iron Contributor
Hi TJCooper
Here you go:
$PrivAccounts = Get-ADUser -Filter * -SearchBase "OU=ABAccounts,OU=DOT,DC=test,DC=LOC"
foreach ($PrivAccount in $PrivAccounts)
{
$SamAccountName = $PrivAccount.SamAccountName
Write-Host "Working on: $SamAccountName"
#Remove "AB-" Prefix
If ($SamAccountName -like "AB-*")
{
$NormalAccount = $SamAccountName.Substring(3)
$ADUser = Get-ADUser -Identity $NormalAccount
If ($ADUser.Enabled -eq $True)
{
Write-Host "$(ADUser.SamAccountName) is Enabled"
} else {
Write-Host "$(ADUser.SamAccountName) is Disabled"
}
}
}
Kind Regards
Andres