Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I transform object properties during output with Select-Object/Export-CSV?

Tags:

powershell

I'm using the Quest AD cmdlets, particularly Get-QADUser, to pull a list of users from AD and return just a few attributes. No problems, easy enough, but I want to transform one of the properties (parentContainerDN) before exporting to CSV.

Get-QADUser -name "Froosh" | Select-Object logonName,homeDrive,parentContainerDN | Export-CSV C:\Temp\File.csv

This works of course, but the parentContainerDN is long and untidy. Is there an easy way to replace that with parentContainerDN.Name before passing that to Export-CSV?

I'd be happy with a commandline solution, or a script snippet.

Thanks!

like image 941
Froosh Avatar asked Oct 16 '08 01:10

Froosh


People also ask

How do you select an object in PowerShell?

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.

What is Export-CSV?

Most often understood as an acronym for “comma-separated values” (though sometimes called “character-separated values” because the separator character does not have to be a comma), CSV is a file format that stores tabular data in plain-text form.


1 Answers

There's a special syntax to create on-the-fly properties in select-object. Try this (wrapping added for clarity):

get-qaduser -name "hamilmat" 
    | select-object logonName, homeDrive, 
        @{Name="containerName"; Expression={$_.parentContainerDN.Name}} 
    | export-csv ...
like image 128
Matt Hamilton Avatar answered Oct 06 '22 00:10

Matt Hamilton