Share via


How to Add Columns to an Array

Question

Friday, September 23, 2016 4:04 PM

I'm working on a script for creating new users from a CSV that at various points needs to connect to Active Directory, Local Exchange, Exchange Online, and Office 365 (Based on this article https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/). Before Office 365, I just Imported the CSV then created variables in a For loop (Import-Csv "NewUsers.csv" | ForEach-Object), but now I need to be able to parse my variables into Remote sessions and the like, so I would like to save values to, I believe I need an array or a hash table. I'm not sure what the difference is.

I have a CSV with first names and last names. I would like to import that CSV to an array, then save the following variables as new columns for each user in the array.

$fullName = $firstName + " " + $lastName
$upn = $firstName + "." + $lastName + "@" + $domainName
$alias = $firstName + "." + $lastName
$sam = $firstName + "." + $lastName

So it would have headers of firstname, lastname, upn, alias, sam, etc...

Second question, how do you bring the array into a New-PSSession and do you have to do anything special to reference it there.

All replies (5)

Friday, September 23, 2016 4:12 PM ✅Answered

There's really no need to create another file, but here's an example:

Import-Csv .\nameList.csv | ForEach-Object {

    $props = @{
        FullName = "$($_.FirstName) $($_.LastName)"
        UserPrincipalName = "$($_.FirstName).$($_.LastName)@domain.com"
        Alias = "$($_.FirstName).$($_.LastName)"
        SamAccountName = "$($_.FirstName).$($_.LastName)"
    }

    New-Object PsObject -Property $props

} | Export-Csv .\userList.csv -NoTypeInformation


Friday, September 23, 2016 5:34 PM ✅Answered

$csv = Import-Csv mycsv.csv | select *,@{n='UPN;e={$firstName +'.' +$lastName + "@$domainName"}}, ... other properties

\(ツ)_/


Friday, September 23, 2016 4:19 PM

Thanks for your really quick reply. I'm not really looking to create a second CSV file. I want to add all of those to the array that I imported. I need to take the array and run a check against AD to make sure duplicate users aren't created, then run New-RemoteMailbox, then I need to take all of that data into a New-PSSession for some licensing stuff. I need to be able to pull this data through to different places. I suppose I could write it to a CSV then import it again with the new entries for upn and such, but that seems a bit indirect. Does that make sense?


Friday, September 23, 2016 4:24 PM

What array?  A CSV is an array of custom objects.  YOU can add columns to the CSV when you load it.

$csv = Import-Csv mycsv.csv | select *,NewCol1,NewCol2 ...

\(ツ)_/


Friday, September 23, 2016 5:10 PM

So we start with two values that we get from a CSV, firstname and lastname. There are no other values in the CSV and I don't want to manually enter them there. I want to import the CSV to an Array or Hash Table or whatever, then use a script to add more columns to it for upn, sam, alias, password, and everything else you need for a mailbox based on a formula in the script.