PowerShell: Lock User Accounts inside an OU

I am currently working on a PowerShell script that will tell you what accounts are locked out and the OU they are in. You can now check this out here.

I have a script that will lock user accounts inside an OU. This is good for testing the above program.

#Requires -Version 3.0
#Requires -Modules ActiveDirectory, GroupPolicy
if ($LockoutBadCount = ((([xml](Get-GPOReport -Name "Default Domain Policy" -ReportType Xml)).GPO.Computer.ExtensionData.Extension.Account |
Where-Object name -eq LockoutBadCount).SettingNumber)) {
$Password = ConvertTo-SecureString 'NotMyPassword' -AsPlainText -Force
Get-ADUser -Filter * -SearchBase "OU=Test,DC=IT,DC=com" -Properties SamAccountName, UserPrincipalName, LockedOut |
ForEach-Object {
for ($i = 1; $i -le $LockoutBadCount; $i++) {
Invoke-Command -ComputerName hyperv {Get-Process
} -Credential (New-Object System.Management.Automation.PSCredential ($($_.UserPrincipalName), $Password)) -ErrorAction SilentlyContinue
}
Write-Output "$($_.SamAccountName) has been locked out: $((Get-ADUser -Identity $_.SamAccountName -Properties LockedOut).LockedOut)"
}
}

LockAccountsOU

Advertisements

2 thoughts on “PowerShell: Lock User Accounts inside an OU

  1. I personally don’t like Powershell. I used to write batch files in MS-DOS. Currently, C++ and Powerbasic. It has structure, syntax, and modularity. Powershell, however many things one can do with it, if I was without a book or internet connection, I would be in a jambalaya tree of ?}{.- -$ symbols thrown around haphazardly. It has no real programming structure one can remember off hand.

    Like

    • Hi Camille,
      It is not true programming language, but it is a scripting language that is based on .Net. PowerShell is a great tool when working with Windows OS and soon Linux using .Net Core. I think we would all be lost without internet 😛

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s