Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Powershell script displays Disabled and enabled Users - Receiving error that user does not exist

$
0
0

Hello,

I currently have a PS script that shows Disabled and Enabled users within Active Directory. Everything works fine; except for the fact if the user does not exist, it will display the following error within the console:

Get-ADUser : Cannot find an object with identity: 'test1'

I am green with PS and I need help adding an else if statement. If the user is not found, I want it to show "User not found". My results are exported to a csv file.  

Here is my code:

$userlist = Import-Csv "C:\Scripts\AD User Status\users1.csv"
foreach($user in $userlist){
    Get-ADUser -Server "My Domain" -Identity $user.username -Properties * | Select DisplayName, SAMAccountName, Enabled |
    Export-Csv "C:\Scripts\AD User Status\UserStatus.csv" -NoTypeInformation -Append
    }

I appreciate your help!

:)



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>