Forum Discussion

VidRocksKay's avatar
VidRocksKay
Copper Contributor
Aug 29, 2022
Solved

Export a combined (from two different sources) result as a CSV using PowerShell

Hi Guys,    I have a requirement to fetch Office 365 users based on a CSV file column (Display Name) and finally update the rest of the columns in it (UPN, First Name, Last Name, IsLicensed etc..) ...
  • Harm_Veenstra's avatar
    Aug 29, 2022

    VidRocksKay I changed your script a little bit 🙂 

     

    $Import = Import-Csv D:\temp\users.csv -Delimiter ';'
    $Total = @()
    foreach ($item in $Import) {
        $MS = Get-MsolUser -SearchString $Item.DisplayName
    
        $msoluser = [PSCustomObject]@{
            'UserPrincipalName' = $MS.UserPrincipalName
            'IsLicensed'        = $MS.IsLicensed
            'BlockCredential'   = $MS.BlockCredential
            'FirstName'         = $MS.FirstName
            'LastName'          = $MS.LastName
            'ArchiveID'         = $item.ArchiveID
            'DisplayName'       = $item.DisplayName
        }
        $Total += $msoluser
    }
    $Total | Select-Object DisplayName, ArchiveID, UserPrincipalName, IsLicensed, BlockCredential, FirstName, LastName | Export-Csv -NoTypeInformation "D:\Temp\NewOutput.csv" -Delimiter ';' -Encoding UTF8

     

    Changed it to a pscustomobject and added every item to a Total variable and saved it to a .csv (Used ; as delimiter, for you it could be a , ) You also had a ArchiveName but I didn't see it in your screenshot, so I removed if for testing but you could add it again 🙂 

    My users.csv file:

    Displayname; ArchiveID
    Alex Wilber;1
    Allan Deyoung;2

     

    I ran this in my CDX environment for these two users, Excel output:

     

Resources