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.
This topic is 3 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
kiidstuuff123
Posts: 3
Last visit: Thu Oct 28, 2021 5:00 pm

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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
This topic is 3 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked