# How to export from foreach-object

**URL:** <https://forums.powershell.org/t/how-to-export-from-foreach-object/13001>\
**Category:** PowerShell Help\
**Created:** [August 15, 2019, 7:50am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001 "2019-08-15T07:50:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 15, 2019, 7:50am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/1 "2019-08-15T07:50:41Z")

</div>

Hi Everyone,

&nbsp;

I am trying to export the output of creating new azure AD users to a csv file.

&nbsp;

This is what I’ve come up with so far:

&nbsp;

# 

$users = Import-Csv “C:\test\users.csv”

# 

#Create Password

&nbsp;

Function Generate-Password(){

Param($max = 1)

For ($i = 0; $i -lt $max; $i++){

$pw = Get-Random -Count 1 -InputObject ((65..72)+(74..75)+(77..78)+(80..90)) | % -begin {$UC=$null} -process {$UC += [char]$\_} -end {$UC}

$pw += Get-Random -Count 2 -InputObject (97..122) | % -begin {$LC=$null} -process {$LC += [char]$\_} -end {$LC}

$pw += Get-Random -Count 2 -InputObject (48..57) | % -begin {$NB=$null} -process {$NB += [char]$\_} -end {$NB}

$pw += Get-Random -Count 3 -InputObject (97..122) | % -begin {$LC=$null} -process {$LC += [char]$\_} -end {$LC}

&nbsp;

write-output $pw

}

}

&nbsp;

#Create new AzureAD User

$users = Import-Csv “C:\test\users.csv”

&nbsp;

$mail = ($user.Firstname + “.” + $user.lastname).ToLower()

$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile

$password = Generate-Password 1

$PasswordProfile.Password = $password

&nbsp;

$users | ForEach-Object {New-AzureADUser -GivenName $user.Firstname `

-MailNickName $mail`

-UserPrincipalName $user.Username`

-DisplayName $user.DisplayName`

-Surname $user.lastname`

-JobTitle $user.JobTitle`

-Department $user.Department`

-AccountEnabled $true `

-PasswordProfile $PasswordProfile} | Export-Csv -Path “C:\test\output.csv” -NoTypeInformation

&nbsp;

I did originally try to use a foreach loop but that does not use the pipeline to get the data out. Hence why I’m using the ForEach-Object commandlet. I think my issue is that the variables prior to running the ForEach-Object need to be parsed but don’t think I’m doing it right.

&nbsp;

Any help much appreciated.

&nbsp;

Darren

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 15, 2019, 8:25am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/2 "2019-08-15T08:25:25Z")

</div>

Please read the information in the very first post of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/) … especially the how to post code. 😉

I’d recommend something like this:

```
Function New-Password {
Param($max = 1)
For ($i = 0; $i -lt $max; $i++) {
$pw = Get-Random -Count 1 -InputObject ((65…72) + (74…75) + (77…78) + (80…90)) | ForEach-Object -begin { $UC = $null } -process { $UC += [char]$_ } -end { $UC }
$pw += Get-Random -Count 2 -InputObject (97…122) | ForEach-Object -begin { $LC = $null } -process { $LC += [char]$_ } -end { $LC }
$pw += Get-Random -Count 2 -InputObject (48…57) | ForEach-Object -begin { $NB = $null } -process { $NB += [char]$_ } -end { $NB }
$pw += Get-Random -Count 3 -InputObject (97…122) | ForEach-Object -begin { $LC = $null } -process { $LC += [char]$_ } -end { $LC }
write-output $pw
}
}

$UserList = Import-Csv ‘C:\test\users.csv’  
$Result = foreach ($User in $UserList) {  
$mail = ($user.Firstname + “.” + $user.lastname).ToLower()  
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile  
$password = New-Password 1  
$PasswordProfile.Password = $password  
$Params = @{  
GivenName = $user.Firstname  
MailNickName = $mail  
UserPrincipalName = $user.Username  
DisplayName = $user.DisplayName  
Surname = $user.lastname  
JobTitle = $user.JobTitle  
Department = $user.Department  
AccountEnabled = $true  
PasswordProfile = $PasswordProfile  
}  
New-AzureADUser @Params  
[PSCustomObject]@{  
GivenName = $user.Firstname  
MailNickName = $mail  
UserPrincipalName = $user.Username  
DisplayName = $user.DisplayName  
Surname = $user.lastname  
JobTitle = $user.JobTitle  
Department = $user.Department  
AccountEnabled = $true  
PasswordProfile = $password  
}  
}  
$Result | Export-Csv -Path ‘C:\test\output.csv’ -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 15, 2019, 8:36am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/3 "2019-08-15T08:36:07Z")

</div>

Hello Olaf,

thank you for your quick response.

will that also collect the passwords that I tried to randomly create or do I need to merge the outputs somehow?

Kind regards, Darren

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 15, 2019, 9:07am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/4 "2019-08-15T09:07:37Z")

</div>

I don’t have any experience with Azure AD cmdlets yet but it should output whatever the cmdlet’s output is … if you like to explicitly output something particular you might add an additional PSCustomObject ( I updated the code example above ). Of course you should try the code in a lab environment - not in your production environment - to verify it does what you expect. 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 15, 2019, 10:18am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/5 "2019-08-15T10:18:34Z")

</div>

Hi Olaf,

thank you for your reply.

I’ve done some quick testing and it returns these column headings:

GivenName, MailNickName, UserPrincipalName, DisplayName, Surname, JobTitle, Department, AccountEnabled, PasswordProfile

The column titled PasswordProfile, returns the value “Class” for every user.

Is there a way of adding another column with the randomly generated password?

Kind Regards, Darren

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 15, 2019, 3:01pm UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/6 "2019-08-15T15:01:23Z")

</div>

You’re creating the actual password as the variable **$password** and you’re adding the actual password as a subproperty of **$PasswordProfile** with the name **Password** - so that’s **$PasswordProfile.Password**. If you want to output this you have to either output the content of the variable **$password** or the subproperty **$PasswordProfile.Password** … that’s totally up to you. 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 16, 2019, 5:40am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/7 "2019-08-16T05:40:10Z")

</div>

Hi Olaf,  
Thanks for your response.  
To Summarise:  
The password is being created in $password  
The password is being added to $passwordProfile with the name “Password”

**OUTPUT**  
$password  
or  
$PasswordProfile.Password

**OPTIONS**  
Get-Variable - gets variables displays on console, can be piped to external output.  
Write-Output - displays on console  
Output-File - sends output to a file.  
_Export-csv - directs to a csv file (prefered)_

**TEST**  
$PasswordProfile.Password | Export-csv/path/file.csv

**RESULT**  
“Length”  
“8”

I suppose the question is, why does the csv only contain this data and not the passwords?

How can I get the passwords into the csv file?

Kind regards, Darren

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 16, 2019, 5:54am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/8 "2019-08-16T05:54:16Z")

</div>

Hmmm … I don’t know what exactly the problem for you is but I tested the following code and it works just as expected:

```
Function New-Password {
Param($max = 1)
For ($i = 0; $i -lt $max; $i++) {
$pw = Get-Random -Count 1 -InputObject ((65…72) + (74…75) + (77…78) + (80…90)) | ForEach-Object -begin { $UC = $null } -process { $UC += [char]$_ } -end { $UC }
$pw += Get-Random -Count 2 -InputObject (97…122) | ForEach-Object -begin { $LC = $null } -process { $LC += [char]$_ } -end { $LC }
$pw += Get-Random -Count 2 -InputObject (48…57) | ForEach-Object -begin { $NB = $null } -process { $NB += [char]$_ } -end { $NB }
$pw += Get-Random -Count 3 -InputObject (97…122) | ForEach-Object -begin { $LC = $null } -process { $LC += [char]$_ } -end { $LC }
write-output $pw
}
}

$List = Get-ADUser -SearchBase ‘…’ -Filter \*  
$Result = foreach ($User in $List) {  
$Password = New-Password 1  
[PSCustomObject]@{  
GivenName = $user.Givenname  
UserPrincipalName = $user.UserPrincipalName  
DisplayName = $user.Name  
Surname = $user.Surname  
PasswordProfile = $Password  
}  
}  
$Result | Format-Table -AutoSize
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 16, 2019, 6:30am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/9 "2019-08-16T06:30:41Z")

</div>

Olaf,

As I’m using Get-AzureADUser instead of Get-ADUser I amended line 12 as follows:

```
$List = Get-AzureADUser -SearchString '.........' -Filter *
```

When I run this, here is the result:

```
$Result | Format-Table -AutoSize | Export-Csv -Path 'C:\path\file.csv' -NoTypeInformation
Get-AzureADUser : Parameter set cannot be resolved using the specified named parameters.
At line:12 char:9
+ $List = Get-AzureADUser -SearchString '.........' -Filter *
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzureADUser], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.Open.AzureAD16.PowerShell.GetUser
```

The reason I changed Line 12 is that ‘SearchBase’ is not valid with Get-AzureADUser where as ‘SearchString’ is.

The message in the result “Parameter set cannot be resolved using the specified named parameters.” I’ve checked the online help for _Get-AzureADUser_ and those parameters are valid but not at the same time:

```
Get-AzureADUser
[**-SearchString** <String>]
[-All <Boolean>]
[<CommonParameters>]

Get-AzureADUser
[-All <Boolean>]
[-Top <Int32>]
[**-Filter** <String>]
[<CommonParameters>]
```

Unless my understanding of the information in help is incorrect?

Darren

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 16, 2019, 7:51am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/10 "2019-08-16T07:51:43Z")

</div>

That code snippet was just to show that the password will be outputted as expected.

I recommend to make a step back and start to learn the very basics of Powershell. This way you’re able to understand the help you get in forums like this and you can adapt the code to your specific environment.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 16, 2019, 9:09am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/11 "2019-08-16T09:09:19Z")

</div>

Hi Olaf,

thank you for your help with this thread.

It is much appreciated.

I have been through a few books including Dons PowerShell in a Month of lunches.

Is there another resource which would be of help?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 16, 2019, 9:45am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/12 "2019-08-16T09:45:13Z")

</div>

Hmmm … I think that should be a good base … now it is practice, practice, practice … 😉

Edit: I updated the code again. It should output the password as well.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [August 16, 2019, 10:48am UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/13 "2019-08-16T10:48:46Z")

</div>

Here’s a trick to pipe from foreach () :

```
PS C:\users\me> & { foreach ($i in dir | where extension -eq .txt) { $i } } | measure

Count : 98
Average :
Sum :
Maximum :
Minimum :
Property :
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/how-to-export-from-foreach-object/13001/14 "2024-05-16T20:31:48Z")

</div>


