Public/Find-DisabledAccount.ps1
Function Find-DisabledAccount { <# .Synopsis Find disabled ActiveDirectory Accounts .Description Find disabled ActiveDirectory Accounts, this is a modified version of the Search-ADAccount CmdLet it only returns useracount information. This version does not show accounts that are disabled by default (such as the Guest account). .Parameter AuthType Specifies the authentication method to use. Possible values for this parameter include: Negotiate or 0 Basic or 1 The default authentication method is Negotiate. A Secure Sockets Layer (SSL) connection is required for the Basic authentication method. The following example shows how to set this parameter to Basic. -AuthType Basic .Parameter Credential Specifies the user account credentials to use to perform this task. The default credentials are the credentials of the currently logged on user unless the cmdlet is run from an Active Directory PowerShell provider drive. If the cmdlet is run from such a provider drive, the account associated with the drive is the default. To specify this parameter, you can type a user name, such as "User1" or "Domain01\User01" or you can specify a PSCredential object. If you specify a user name for this parameter, the cmdlet prompts for a password. You can also create a PSCredential object by using a script or by using the Get-Credential cmdlet. You can then set the Credential parameter to the PSCredential object The following example shows how to create credentials. $AdminCredentials = Get-Credential "Domain01\User01" The following shows how to set the Credential parameter to these credentials. -Credential $AdminCredentials If the acting credentials do not have directory-level permission to perform the task, Active Directory PowerShell returns a terminating error. .Parameter ResultPageSize Specifies the number of objects to include in one page for an Active Directory Domain Services query. The default is 256 objects per page. The following example shows how to set this parameter. -ResultPageSize 500 .Parameter ResultSetSize Specifies the maximum number of objects to return for an Active Directory Domain Services query. If you want to receive all of the objects, set this parameter to $null (null value). You can use Ctrl+c to stop the query and return of objects. The default is $null. The following example shows how to set this parameter so that you receive all of the returned objects. -ResultSetSize $null .Parameter SearchBase Specifies an Active Directory path to search under. When you run a cmdlet from an Active Directory provider drive, the default value of this parameter is the current path of the drive. When you run a cmdlet outside of an Active Directory provider drive against an AD DS target, the default value of this parameter is the default naming context of the target domain. When you run a cmdlet outside of an Active Directory provider drive against an AD LDS target, the default value is the default naming context of the target LDS instance if one has been specified by setting the msDS-defaultNamingContext property of the Active Directory directory service agent (DSA) object (nTDSDSA) for the AD LDS instance. If no default naming context has been specified for the target AD LDS instance, then this parameter has no default value. The following example shows how to set this parameter to search under an OU. -SearchBase "ou=mfg,dc=noam,dc=corp,dc=contoso,dc=com" When the value of the SearchBase parameter is set to an empty string and you are connected to a GC port, all partitions will be searched. If the value of the SearchBase parameter is set to an empty string and you are not connected to a GC port, an error will be thrown. The following example shows how to set this parameter to an empty string. -SearchBase "" .Parameter SearchScope Specifies the scope of an Active Directory search. Possible values for this parameter are: Base or 0 OneLevel or 1 Subtree or 2 A Base query searches only the current path or object. A OneLevel query searches the immediate children of that path or object. A Subtree query searches the current path or object and all children of that path or object. The following example shows how to set this parameter to a subtree search. -SearchScope Subtree .Parameter Server Specifies the Active Directory Domain Services instance to connect to, by providing one of the following values for a corresponding domain name or directory server. The service may be any of the following: Active Directory Lightweight Domain Services, Active Directory Domain Services or Active Directory Snapshot instance. Domain name values: Fully qualified domain name Examples: corp.contoso.com NetBIOS name Example: CORP Directory server values: Fully qualified directory server name Example: corp-DC12.corp.contoso.com NetBIOS name Example: corp-DC12 Fully qualified directory server name and port Example: corp-DC12.corp.contoso.com:3268 The default value for the Server parameter is determined by one of the following methods in the order that they are listed: -By using Server value from objects passed through the pipeline. -By using the server information associated with the Active Directory PowerShell provider drive, when running under that drive. -By using the domain of the computer running Powershell. The following example shows how to specify a full qualified domain name as the parameter value. -Server "corp.contoso.com" .Example Find-DisabledAccount Generates a list of disabled users .Example Find-DisabledAccount | Export-Csv disabledaccounts.csv Exports a list of disabled users to csv file .Inputs None .Outputs Microsoft.ActiveDirectory.Management.ADUser[] .LINK about_functions_advanced .LINK about_CommonParameters .LINK Search-ADAccount #> [CmdletBinding( ConfirmImpact = "Medium", SupportsPaging = $true, PositionalBinding = $false )] Param ( [ValidateSet("Basic", "1", "Negotiate", "0")] [Microsoft.ActiveDirectory.Management.ADAuthType]$AuthType = "Negotiate", [PSCredential][System.Management.Automation.Credential()]$Credential, [Parameter( ValueFromPipeline = $true, HelpMessage = 'Provide the SearchBase' )] [string]$SearchBase, [int]$ResultPageSize = 256, [int]$ResultSetSize = $null, [ValidateSet('Base', 'OneLevel', 'Subtree')] [Microsoft.ActiveDirectory.Management.ADSearchScope]$SearchScope = "Subtree", [string]$Server ) Begin { If (-not $PSBoundParameters.ContainsKey('Debug')) { $DebugPreference = $PSCmdlet.SessionState.PSVariable.GetValue('DebugPreference') } If (-not $PSBoundParameters.ContainsKey('Verbose')) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') } If (-not $PSBoundParameters.ContainsKey('WhatIf')) { $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference') } If (-not $PSBoundParameters.ContainsKey('Confirm')) { $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference') } If (-not $PSBoundParameters.ContainsKey('ErrorAction')) { $ErrorActionPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ErrorActionPreference') } If (-not $PSBoundParameters.ContainsKey('WarningAction')) { $WarningPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WarningPreference') } If (-not $PSBoundParameters.ContainsKey('InformationAction')) { $WarningPreference = $PSCmdlet.SessionState.PSVariable.GetValue('InformationPreference') } } Process { If ($PSBoundParameters['SearchBase']) { $SBText = " using searchbase: {0}" -f $SearchBase } Else { $SBText = "" } Write-Verbose ("$($MyInvocation.MyCommand.Name):: Retrieving users from ActiveDirectory{1}" -f $SBText) Try { $SearchADUsers = @{ "AuthType" = $AuthType "SearchScope" = $SearchScope "ResultPageSize" = $ResultPageSize "AccountDisabled" = $true "UsersOnly" = $true "ErrorAction" = "Stop" } If ($PSBoundParameters['Credential']) { $SearchADUsers.Credential = $PSBoundParameters['Credential'] } If ($PSBoundParameters['SearchBase']) { $SearchADUsers.SearchBase = $PSBoundParameters['SearchBase'] } If ($PSBoundParameters['ResultSetSize']) { $SearchADUsers.ResultSetSize = $PSBoundParameters['ResultSetSize'] } If ($PSBoundParameters['Server']) { $SearchADUsers.Server = $PSBoundParameters['Server'] } Write-Verbose "$($MyInvocation.MyCommand.Name):: Searching ActiveDirectory" $Users = Search-ADAccount @SearchADUsers | Where-Object { $_.Name -notmatch "Migration*" -and $_.Name -notmatch "systemmailbox*" ` -and $_.Name -notmatch "Federated*" -and $_.Name -notmatch "Support_*" ` -and $_.Name -notmatch "DiscoverySearch*" -and $_.Name -notmatch "krb*" ` -and $_.Name -notmatch "Guest" -and $_.Name -notmatch "Exchange*" } } Catch [System.Security.Authentication.AuthenticationException] { Write-Warning "Cannot connect to Active Directory. Invalid credentials" } Catch [System.Management.Automation.CommandNotFoundException] { Write-Warning $_.Exception.Message } Catch [Microsoft.ActiveDirectory.Management.ADServerDownException] { Write-Warning "Cannot connect to Active Directory, are the services running?" } Catch { Get-ErrorInfo $_ } } End { Return $Users } } Set-Alias -Name Get-DisabledAccount -Value Find-DisabledAccount -Description "Find Disabled ActiveDirectory Accounts" -Option ReadOnly -PassThru -ErrorAction SilentlyContinue |