PowerShell Fundamentals for System Admins
Viewing and Exporting PowerShell Queries
Perhaps you would like to view your data outside of the PowerShell command window, or you would like to export results to a file.
One way to review your data outside of the powershell command window is to utilize the grid view. This can make reviewing bulk data a little easier than scrolling through the command window. We can use a previous example of querying ADGroup membership to demonstrate using the grid view for reviewing results. For example:
Get-ADGroupMember GroupName |Select-Object name |Out-GridView
This will display a window where you can review the specified properties that you have chosen. You can use the Out-GridView Command for some of the previous examples that I have covered, for example:
Get-ADComputer Computername -Properties * |Select-Object CN,Description |Out-Gridview
But let’s assume that you would rather have a file containing your results that you can work with, such as a text file. There are a couple of ways of achieving this. For example, you can utilize the out-file command and insert a file path for your file. Using our ADGroupMember example from above, it may look something like this:
Get-ADGroupMember GroupName |Select-Object name |Out-File C:\Users\YOURUSERNAME\Documents\OUTFILE.TXT
Exporting CSV Files
You can also export to .csv file types with the method above. However, I sometimes find that the data isn’t always “user-friendly” via this method for .csv files. For that reason, I’d like to cover the Export-CSV command. Using the same example as above with the export-csv command looks like this:
Get-ADGroupMember GroupName |Select-Object name |Export-csv C:\Users\YOURUSERNAME\Documents\OUTFILE.CSV -noTypeInformation
Notice the “-noTypeInformation” parameter at the end of the command. This excludes the “Type information” from being inserted as a header for the exported file. Unless you need the Type information (you probably do not), I recommend using the -noTypeInformation parameter at the end of your export-csv command. For an in-depth analysis about Type information, check out this article.
Reference
Active Directory. (2022). In Microsoft. Retrieved from https://learn.microsoft.com/en-us/powershell/module/activedirectory/?view=windowsserver2022-ps
Ashiedu, V. (2020, July 30). PowerShell NoTypeInformation Parameter Explained. In iTG. Retrieved from https://www.itechguides.com/powershell-notypeinformation/