Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Another Empty Pipe Error when Export CSV used

$
0
0

You can’t pipe the result of a foreach loop to anything (which is different from the ForEach-Object cmdlet). There are a couple of ways you could change your code:

Preferred method (not saving the entire contents of the Users.txt file in memory, but processing one line at a time:

# Iterate through list using Foreach

Get-Content c:\inputfiles\Users.txt |
ForEach-Object {
# Grab Username, ExServName, Applicable Mailbox info
     get-aduser $_ -property name, msExchHomeServerName, mDBStorageQuota, mDBOverQuotaLimit, mDBOverHardQuotaLimit, mDBUseDefaults | select name, msExchHomeServerName, mDBStorageQuota, mDBOverQuotaLimit, mDBOverHardQuotaLimit, mDBUseDefaults 

# Export results to out.csv
} | export-csv out.csv

Another option to force a foreach loop to pipe its results somewhere (placing the loop into a subexpression, and piping the results of the subexpression to the next command):

# Grab Users from List

$Userlist = Get-Content c:\inputfiles\Users.txt

# Iterate through list using Foreach

$(
    foreach($User in $UserList) {

        # Grab Username, ExServName, Applicable Mailbox info
         get-aduser $User -property name, msExchHomeServerName, mDBStorageQuota, mDBOverQuotaLimit, mDBOverHardQuotaLimit, mDBUseDefaults | select name, msExchHomeServerName, mDBStorageQuota, mDBOverQuotaLimit, mDBOverHardQuotaLimit, mDBUseDefaults 

        # Export results to out.csv
    }
) | export-csv out.csv

And a third option, the same sort of idea (instead of a subexpression, placing the foreach loop into a script block and invoking it, then piping the results of that script block to the next command):

# Grab Users from List

$Userlist = Get-Content c:\inputfiles\Users.txt

# Iterate through list using Foreach

& {
    foreach($User in $UserList) {

       # Grab Username, ExServName, Applicable Mailbox info
        get-aduser $User -property name, msExchHomeServerName, mDBStorageQuota, mDBOverQuotaLimit, mDBOverHardQuotaLimit, mDBUseDefaults | select name, msExchHomeServerName, mDBStorageQuota, mDBOverQuotaLimit, mDBOverHardQuotaLimit, mDBUseDefaults 

       # Export results to out.csv
    }
} | export-csv out.csv

Viewing all articles
Browse latest Browse all 13067

Trending Articles