Lock-UserAccount.ps1
19 Sep 2025Description
Purpose
Lock AD User Account.
Detailed Description
This function will lock a user account in Active Directory.
Usage
Example 1
Lock-UserAccount -SamAccountName "jdoe"
Notes
The user account running this function, needs to have ‘Domain Admin Privileges’ in order to lock the account.
Script
function Lock-UserAccount {
<#
.SYNOPSIS
Lock AD User Account.
.DESCRIPTION
This function will lock a user account in Active Directory.
.PARAMETER SamAccountName
The SamAccountName of the user account to be locked.
.NOTES
The user account running this function, needs to have 'Domain Admin Privileges' in order to lock the account.
.EXAMPLE
Lock-UserAccount -SamAccountName "jdoe"
#>
[CmdletBinding(SupportsShouldProcess = $true)]
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$SamAccountName
)
begin {
Write-Verbose "Locking user account '$SamAccountName'..."
}
process {
if ($PSCmdlet.ShouldProcess("$SamAccountName", "Locking user account")) {
$user = Get-ADUser -Identity $SamAccountName
if ($user) {
Set-ADAccountLockout -Identity $user.DistinguishedName -LockoutTime ([timespan]::MaxValue).Days -Confirm:$false
Write-Output "User account '$SamAccountName' has been locked."
}
else {
Write-Error "User account '$SamAccountName' not found."
}
}
}
end {
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.