Search code examples
powershellazure-powershell

How can I parse the output of cmdlet to another?


I need to create a list of no longer active users in the OnPremise Active Directory?

Using the below format as input:

User Name; user.Name@domain.com
John Doe; John.Doe@domain.net
Veki Jado; Veki.Jado@office.com
....

My goal here is to generate the list of which users can be safely removed by Paris g the above into:

Get-ADUser | Export-CSV

How can I achieve that?


Solution

  • If I understood your question correctly, you would like to generate a list of inactive (disabled) Active Directory Users and output them into a CSV file?

    Your statement of

    Using the below format as input:

    is unclear to me though. Is that the format you want your output CSV file to look like, or is it an input containing users informations for which you want to verify if they are still enabled in Active Directory?

    Assuming it is the definition what your output should look like, you would achieve that with the following line of PowerShell code:

    Get-ADUser -Filter {Enabled -eq $false} | Select-Object -Property DisplayName,UserPrincipalName | Export-Csv -Path "C:\temp\myoutputfile.csv" -Delimiter ";" -Encoding UTF8 -NoTypeInformation
    

    Edit 1:

    Based on your elaborations, I was able to reproduce the output that is actually really an array of strings formatted like this:

    FirstName LastName;firstname.lastname@domain.com

    So the following code should give you what you are looking for:

    $theInputCollectionOfUsers = Get-AntiPhishPolicy -Identity 'Default anti-phishing policy' | Select-Object -ExpandProperty TargetedUsersToProtect
    
    [array]$adUsers = $null
    foreach($user in $theInputCollectionOfUsers){
    $userPrincipalName = $user.Split(";")[1]
    [array]$adUsers += Get-ADUser -Identity $userPrincipalName
    }
    $adUsers | Select-Object -Property DisplayName,UserPrincipalName,Enabled | Export-Csv -Path "C:\temp\myoutputfile.csv" -Delimiter ";" -Encoding UTF8 -NoTypeInformation