active directory show locked users which are not expired - powershell

Is there a way to show the locked users, not disabled (if the password has typed wrong 3 time), which are are not expired (account not expired) either with AD query or a powershell script.
I searched on the net but couldn't find a solution.

This should work if your looking for locked out accounts which are not yet expired or without an expiration date set:
Search-ADAccount -LockedOut | where { $_.AccountExpirationDate -gt (Get-Date) -or $_.AccountExpirationDate -eq $null }

There is a way. You could import ActiveDirectory module and use Search-ADAccount cmdlet.
Search-ADAccount -LockedOut -UsersOnly | Where-Object { $_.PasswordExpired -eq $false }

Related

Filtering "DistinguishedName" Output with Search-ADAccount

I'm trying to search through AD using the Search-ADAccount cmdlet to find accounts which are Enabled but haven't logged in for the last 90 days. The below commands adequately finds enabled AD accounts on the domains which haven't logged in the last 90 days. However, I'm trying to filtering out any entries where the "DistinguishedName" field includes the text of "Service Account".
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 |
Select-Object -Property Name, Enabled, LastLogonDate, DistinguishedName |
Where-Object {
$_.Enabled -like 'True' -and
$_.DistinguishName -notmatch "Service Account"
}
The above command seems to work fine for finding the enabled accounts and last logon. However, whatever text I put in -notmatch "..." seems to be disregarded by PowerShell?
Also tried -notLike and I get the same behavior also.
Thanks boxdog for noticing the mis-spelling of the DisinguishedName in the Where-Object missing the "ed". This fixed the issue in combination with your other comment about the missing *Service Account* wildcards.
Updated PowerShell command:
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 |
Where-Object {
$_.Enabled -like 'True' -and
$_.DistinguishedName -NotLike "*Service Account*"
} |
Select-Object -Property Name,Enabled,LastLogonDate,DistinguishedName

Delete expired accounts in specific OUs

Trying to make a PS script that finds and deletes expired accounts in specific OUs
I've created this script, and it gets the users that is expired in the 4 OUs, so far so good, but I cant get my head around how to make it delete the users.
$OUs=
"OU=1,OU=Users,DC=Test,DC=local",
"OU=2,OU=Users,DC=Test,DC=local",
"OU=3,OU=Users,DC=Test,DC=local",
"OU=4,OU=Users,DC=Test,DC=local"
Foreach($OU in $OUs){
Search-ADAccount -AccountExpired -Searchbase $OU | Select-Object Name
}
Anybody that got a solution for this? :)
This works on my machine
Foreach($OU in $OUs){
Search-ADAccount -AccountExpired -Searchbase $OU | Remove-ADObject -Confirm:$false
}

migrate from get-qaduser to get-aduser

I have setup my script to disable inactive user in my Win 2003 AD server using Quest's AD tool GET-QADUSER, and now I am going to migrate AD to Win 2008 R2. Since there is Active Directory module and Quest's tool is no longer free to download (is that?), I am going to migrate to GET-ADUSER.
I am converting from:
Foreach ($ou in $searchBase) {
#$inactUsr += #(Get-QADUser -SearchRoot $ou -Enabled -PasswordNeverExpires:$false -NotLoggedOnFor $inactiveDays -CreatedBefore $creationCutoff -SizeLimit $sizeLimit | Select-Object Name,SamAccountName,LastLogonTimeStamp,Description,passwordneverexpires,canonicalName | Sort-Object Name)
}
to:
$inactUsr += #(Get-ADUser -SearchRoot $ou -Filter 'enabled -eq $true -and PasswordNeverExpires -eq $False' -Properties Name,SamAccountName,LastLogonTimeStamp,Description,passwordneverexpires,canonicalName | Select Name,SamAccountName,#{N='LastLogonTimeStamp'; E={[DateTime]::FromFileTime($_.LastLogonTimeStamp)}},Description,passwordneverexpires,canonicalName | Sort Name)
I am almost there and leave only -NotLogonFor (which select user that not logon for certain days) and -CreatedBefore (which give a grace period for newly created ID). I want to select ID NotLogon for 30 days and DO NOT want ID created less than 30 days.
Appreciate if anyone can let me know whether there is a built-in properties or any manual method to achieve that.
Edited:
I have the CreatedBefore solved by:
$inactUsrdraft += #(Get-ADUser -SearchBase $ou -Filter 'enabled -eq $true -and PasswordNeverExpires -eq $False -and whenCreated -le $CreationCutOff' -Properties Name,SamAccountName,LastLogonTimeStamp,Description,passwordneverexpires,canonicalName | Select Name,SamAccountName,#{N='LastLogonTimeStamp'; E={[DateTime]::FromFileTime($_.LastLogonTimeStamp)}},Description,passwordneverexpires,canonicalName | Sort Name)
:)
Now I need only need to filter ID not logon more than 30 days.
Any help is appreciated.
How about:
$LastLogonCutoff = (Get-Date).Date.AddDays(-30)
That's midnight 30 days ago. If you want it to the second, use (Get-Date).AddDays(-30).
Followed by changing the -Filter to include:
`-and (LastLogonTimeStamp -lt $LastLogonCutoff)`
Also beware that the LastLogonTimeStamp property is not very accurate. Laptop logins off-network that use saved credentials won't trigger, I believe. If you don't have "Wait for network" enabled, clients might never actually update this value, IIRC.

Search-ADAccount for user accounts with expiring passwords

I've been tasked with finding service accounts (in our shop, that's user accounts starting with 'svc-' in the username) that have expiring passwords. Normally service accounts should not have expiring passwords, so I'm trying to find service accounts incorrectly created so they can be fixed.
I've been using Search-ADAccount and I'm having parameter issues. If I use this:
Search-ADAccount -PasswordNeverExpires | Where {$_.SamAccountName -like 'SVC-*'}
then I get long lists of results, none of which have expiring passwords. But if I'm including the -PasswordNeverExpires parameter, then I'm filtering out any accounts which do have expiring passwords, no?
I've also tried this:
Search-ADAccount | Where {$_.SamAccountName -like 'SVC-*' -and $_.PasswordNeverExpires -like 'FALSE' }
but I only get an error: "Parameter set cannot be resolved using the specified named parameters." That sounds like Search-ADAccount requires certain parameters, but I don't see in the help files which parameters are required.
It's counter-intuitive (to me) that Search-ADAccount has a parameter which can search for one Boolean condition (TRUE) but not the other.
Get-ADUser doesn't seem to have any password configuration info.
Yes, Trondh. That's it. I first looked at Get-ADUser, but the help files didn't mention anything about the PasswordNeverExpires parameter, and piping a single result into Get-Member didn't reveal any relevant property to search against.
In sum, this is the one-liner that worked:
Get-ADUser -filter {PasswordNeverExpires -eq $False} | Where {$_.SamAccountName -like 'SVC-*'}
Thanks again.
I would just use get-aduser (need to clean up the filter param, I just banged this together in my head):
$adusers = Get-ADUser -Filter * -Properties * | where {$_.PasswordNeverExpires -eq $false}
did you try $_.PasswordNeverExpires -eq $false?

Powershell command to hide user from exchange address lists

I'm trying to write powershell script which hides user from exchange lists.
I was able to find following command:
Set-Mailbox -Identity [user id here] -HiddenFromAddressListsEnabled $true
And it doesn't give me an error message, and when I run the command twice, I get following warning:
WARNING: The command completed successfully but no settings of '[user id here]' have been modified.
Which probably means that the command did actually work.
but when I go to Exchange Management Console, and open user profile, "hide user from exchange address lists" check box is off.
What could be the reason?
I use this as a daily scheduled task to hide users disabled in AD from the Global Address List
$mailboxes = get-user | where {$_.UserAccountControl -like '*AccountDisabled*' -and $_.RecipientType -eq 'UserMailbox' } | get-mailbox | where {$_.HiddenFromAddressListsEnabled -eq $false}
foreach ($mailbox in $mailboxes) { Set-Mailbox -HiddenFromAddressListsEnabled $true -Identity $mailbox }
You can use the following script, just replace DOMAIN with the name of your domain. When executed it will prompt you for a userlogin then hide that user's account from the address lists.
$name=Read-Host "Enter login name of user to hide"
Set-Mailbox -Identity DOMAIN\$name -HiddenFromAddressListsEnabled $true
Brian.
I was getting the exact same error, however I solved it by running $false first and then $true.
You will have to pass one of the valid Identity values like DN, domain\user etc to the Set-Mailbox cmdlet. Currently you are not passing anything.
"WARNING: The command completed successfully but no settings of '[user id here]' have been modified."
This warning means the setting was already set like what you want it to be. So it didn't change anything for that object.
For Office 365 users or Hybrid exchange, go to using Internet Explorer or Edge, go to the exchange admin center, choose hybrid, setup, chose the right button for hybrid or exchange online.
To connect:
Connect-EXOPSSession
To see the relevant mailboxes:
Get-mailbox -filter {ExchangeUserAccountControl -eq 'AccountDisabled'
-and RecipientType -eq 'UserMailbox' -and RecipientTypeDetails -ne 'SharedMailbox' }
To block based on the above idea of 0KB size:
Get-mailbox -filter {ExchangeUserAccountControl -eq 'AccountDisabled'
-and RecipientTypeDetails -ne 'SharedMailbox' -and RecipientType -eq 'UserMailbox' } | Set-Mailbox -MaxReceiveSize 0KB
-HiddenFromAddressListsEnabled $true