Disable/Enable AD user account from CSV

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
kiidstuuff123
Posts: 3
Joined: Wed Nov 18, 2020 8:32 am

Disable/Enable AD user account from CSV

Post by kiidstuuff123 »

How can I enable or disable an AD user account from a csv based on an entry. If the status for both say Active, only one account gets enabled instead of both. Same for the disabled status.

CSV file:
Samaccountname,Status
john.doe,Active
jane.doe,Disabled
What I have so far:

$User = Import-Csv -Path c:\folder\adaccounts.csv

ForEach ($User in $Users)
{
IF ($User.Status -contains "Disabled")
{
Get-ADUser -Identity $user.samaccountname | Disable-ADAccount
}
elseif ($User.Status -contains "Active")
{
Get-ADUser -Identity $user.samaccountname | Enable-ADAccount
}
by jvierra » Mon Jan 18, 2021 7:02 am
"-contains" only works for collections. Use "-match" or "-eq".

Here is a good place to start to learn basic PowerShell and programming: https://www.sapien.com/books_training/W ... werShell-4
Go to full post

jvierra
Posts: 14697
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 5 times

Re: Disable/Enable AD user account from CSV

Post by jvierra »

"-contains" only works for collections. Use "-match" or "-eq".

Here is a good place to start to learn basic PowerShell and programming: https://www.sapien.com/books_training/W ... werShell-4

Locked