Practical PowerShell Uncategorized Formatting The Screen and Selecting Text in PowerShell 5.0/4.0

Formatting The Screen and Selecting Text in PowerShell 5.0/4.0

Working in PowerShell 5.0 vs older versions of PowerShell, there are some noticeable changes that are items that one need to be aware of. These changes have to do with the formatting of output from you script, cmdlet or one-liner and also how you would copy output from a PowerShell window. We will cover Line Wrapping in PowerShell as well as some FL and FT nuances.

Line Wrapping

First time that you try to copy data from a PowerShell 5.0 window you might be surprised that you are suddenly highlighting entire sections of the screen that you may not have wanted to highlight. This is because PowerShell 5.0 introduced the concept of line-wrapping when it comes to selected items on the screen.

On a Windows 2012 R2 server, running in a PowerShell 4.x window. After getting a list of users, when attempting to copy a section of the screen output, it works as it has in previous versions of PowerShell:

Now, performing the same actions we see that we cannot select just a small part of the screen output, but we get entire lines of the output:

What? Well, Microsoft made a pretty big change in the properties of the shell window:

Once that value is cleared, then PowerShell behaves like it did in 4.0 and earlier. Now, if you like that setting, then don’t clear it.

Format Table / Default View PowerShell 5.0 vs 4.0

When switching from PowerShell 4.0 to 5.0, you will also see subtle changes in the output of cmdlets and how output is handled. I guess this is expected as Microsoft tries to tweak PowerShell and make it better for the end user/administrator to use. In this case we are referring to how properties for an object are displayed by default for a cmdlet:

[PowerShell 5.0]

[sourcecode language=”powershell”]
Get-ADUser
[/sourcecode]

Notice that the only column that appears is the Distinguished Name property. Why is that? It’s because the screen isn’t wide enough and if we widen the PowerShell window, more fields will be revealed:

[PowerShell 4.0]

Whereas in PowerShell 5.0 the output was geared to full display of a field, PowerShell 4.0 does not do this be default. As can be seen by running the same ‘Get-ADUser’ cmdlet on a Windows 2012 R2 DC, we see the output is geared towards the fields and not necessarily the date contained in those fields:

While this may not be the best option either, in some ways it may be more revealing / useful to get an idea of some of the properties that a particular PowerShell cmdlet may reveal.

Format List

Regardless of which PowerShell version that is being utilized, using Format-List (FL) is most useful when the normal table view of data does not contain the right fields. With FL we can reveal all of the fields of an object we are working with. This way we can pick the right attributed and then use them to construct a table either for visual representation in the PowerShell console or to be used for an export to a CSV file. Format list may also be needed for documentation if one needs all properties of an object for later reference or comparisons.

Example

Let’s take for example a user in Active Directory, which we can list with Get-ADUser. Couple of tips with this cmdlet:

• Use ‘-Properties *’ to see all properties available on a user object
• User ‘-Filter *’ to see all users in AD.

If we were to use ‘| FL’ on the base Get-ADUser cmdlet, we see that there are only ten properties revealed. We know that there are A LOT more that just 10. So, we can use -Properties to reveal this. While we don’t technically need ‘FL’, the effect is the same and simulates what other objects would display.

With this, I can now see I want to grab a couple of properties – DisplayName, Description, EmailAddress
[sourcecode language=”powershell”]
Get-ADUser Damian| FT DisplayName, Description, Department
[/sourcecode]
This would provide a table output for my user account. If we wanted all users, simply remove my name from the one-liner above for that.

Conclusion

As we can see we need to be aware of our formatting and settings as we transition to PowerShell 5.0. FL has not changed, but FT and Line wrapping have.

Related Post