Exporting in PowerShell

I’ll admit it, csv is my preferred input and output format. And why not? Most of the time data has been gathered using Excel.

My favorite output cmdlet in PowerShell has to be out-gridview. Three guesses why? 🙂

Sometimes I might want to export results and at other times, having the results in out-gridview is enough.

My default modus operandi is having results in out-gridview. I like to use the [switch] parameter to explicitly enable exporting to a file.

Most of my scripts end with the following piece of code:

if ($export) {
    $arrExportStatus |  Export-CSV -NoTypeInformation "$pwd\export\$exportCSVSetOwnershipOnFiles" -delimiter $delimiter
else {
    if (!($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent)) {
        $arrExportStatus | Out-GridView -Title "User Homedirectories SetOwnership"

If I gave up the parameter -export, then this will return $true. Because of my script repository and my foolproof LogFileName, I’m ready to export my data with confidence! 😉 (There is method to my madness…) If I didn’t specify -export then I’ll get the results in out-gridview.

You may have noticed that extra line of code, what’s that for? Well I’m glad you asked! Ok so there’s this option [CmdletBinding()]. This gives you access to some cool features like verbose in PowerShell. I won’t get into details right now, but if I specified export AND verbose, then I don’t really need the results in out-gridview. So export parameter will give me the results in a logfile. Without the export option I’ll get the results in out-gridview. If I use the -verbose option then I’ll just receive all verbose information.

Hope it’s worth something to you…




2 thoughts on “Exporting in PowerShell

  1. Pingback: Revisiting Exporting Data | pshirwin

  2. Pingback: File servers configuration report | pshirwin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s