PowerShell

PowerShell for Beginners (Part 7): The Pipe (and many examples to play with)

After the sixth part PowerShell for Beginners (Part 6): PowerShell Profiles and the ISE, we dedicate ourselves to the topics around the pipe. But before we will take a look at this the answer of the sixth exercise. We will also have a quick review what we’ve learned so far in Part 6.

Review (Part 6)

Exercise

Open ISE. Create a PowerShell Profile for all Users. Insert the command Start-Transcript in the first line. Save the file. Open PowerShell. What happend for all users?

This was a very good exercise. Some self-praise ;-). I was able to reconcile two things. Working with ISE and a introducing a new command: Start-Transcript. Well, let’s start with the creation of the PowerShell Profile for all users. As described in the last part we have to specify the all users path.


New-Item $profile.AllUsersAllHosts -Force

Unbenannt.PNG

Next we have to open the created profile. I recommend notepad. Once opened, type Start-Transcript and click Save.

Unbenannt.PNG

Now comes the magic moment. Close notepad and ISE and open PowerShell. As you can see PowerShell is recording now. Start-Transcript records all your inputs.

Unbenannt.PNG

Now it’s up to you to test it. Enter a few commands and open the transcript file. The location of the file can be found in your user profile in the documents folder (as shown in the screenshot).

Summary


If you want to get powershell to do something at startup, create a PowerShell profile file. Be aware of the Execution Policy. The PowerShell ISE is a powerful script editor that can help you writing your scripts.


 

The PowerShell Pipe

Now we come to the next part. Working with the pipe. And this is a key part of the series. Let’s dive in.

The Windows PowerShell pipeline allows you to join two or more statements with a pipe symbol. Working with the pipe is a key technique in PowerShell. You can think of it as a kind of object-forwarding to other PowerShell commands. In concrete terms, the pipe takes everything on the left of the pipe and forwards it to the command to the right of the pipe.

First of all, the pipe symbol is located on your keyboard. You can find it on most keyboards here:

Unbenannt.JPG

So, the goal for now it to combine two commands with a pipe to give you an example.

Review


Working with the pipe is a key technique in PowerShell. In concrete terms, the pipe takes everything on the left of the pipe and forwards it to the command to the right of the pipe. 


 

An example: Get-Process | Stop-Process

Many of you may think: “Hey, that’s nice, but what’s the point?” Now I am going to show you the power of the pipe in a simply example. Let’s say we want to close all Notepad and Paint processes. Well then let’s catch them all and pipe them to Stop-Process.

But wait a minute. Before we begin we take a look at the commands separately. On my computer there are 2 Notepad processes and 2 Paint processes.

Unbenannt.PNG

That said, Get-Process catches all Notepad and all Paint processes. Remember: The pipe takes everything on the left of the pipe and forwards it to the command to the right of the pipe.

So, if we put Get-Process on the left side of the pipe and Stop-Process to the right side of the pipe then all this processes should be stopped.


Get-Process notepad,mspaint | Stop-Process

They are all gone. And if you put -verbose to the end of the command you’ll see that all processes stops immediately and that they all have different process ids.

Unbenannt.PNG

pipe.JPG

Some more example to play with …

Stopping services …


Get-Service spool*,*bits* | Stop-Service -Verbose

Unbenannt.JPG

Showing the newest 5 event logs from the system log in a nicely view with Out-GridView.


Get-EventLog -LogName system -Newest 5 | Out-GridView

Unbenannt.JPG

Browsing the Windows setup log (located at C:\Windows\Panther) and searching the first successful boot entry during installation.


Get-ChildItem C:\Windows\Panther | Select-String 'First Boot'

Unbenannt.JPG

Searching for unique entrys …


Get-Content C:\Temp\file1.txt | Get-Unique

Unbenannt.JPG

or count something …


Get-Content -Path C:\Temp\file1.txt | Get-Unique | Measure-Object

2.JPG

To be more specific:


Get-Content -Path C:\Temp\file1.txt | Get-Unique | Measure-Object | Select-Object Count

Unbenannt.JPG

For the further part I will concentrate on three commands. All of them have to do with formatting and the use of the pipe and they are perfectly suitable for beginners. Let’s move on.

What have we learned so far?


The pipe takes everything on the left of the pipe and forwards it to the command to the right of the pipe.


Piping to Format-Table, Format-List and Format-Wide

Format-Table

The Format-Table cmdlet formats the output of a command. And it’s used with the pipe.

Look at the command below. It shows you a table. The table headers are Mode, LastWriteTime, Length and Name.

unbenannt41.png

This table is pre-defined. But the good news is we can modify the table to fit our needs. Pay attention to the table headers. They have a name. And this names are very important for us. How do you like that:


Get-ChildItem | Format-Table Name,LastWriteTime

Unbenannt.PNG

All you need to know are the header table names. Then you can rearrange the output with Format-Table or you can simply omit things.

Let me give you another example. Get-Service displays all Windows services. Right?

Unbenannt.PNG

But I don’t like this view. I only need the Status and the Name. This is an easy task. Simply pipe it to Format-Table and provide the Status and Name.


Get-Service | Format-Table Status,Name

Unbenannt.PNG

More about Format-Table in the Microsoft Docs:

https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Format-Table?view=powershell-5.1

Format-List

The default output is pre-configured by command. Some commands output too much information and that’s the reason that they are not displayed as a table, but as a list. Get-NetIPConfiguration is such a command.

Unbenannt.PNG

We now know the list headers. Let’s pipe this command to Format-List and select only the IPv6Address and IPv4Address.


Get-NetIPConfiguration | Format-List IPv4Address,IPv6Address

Unbenannt.PNG

Nice. Don’t be distracted by the brackets. In the background a WMI action is started that get’s the IP-Address. That’s what the brackets are for.

We can also format the output of Get-ChildItem to a list.


Get-ChildItem | Format-List Name,LastWriteTime

Unbenannt.PNG

More about Format-List here:

https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Format-List?view=powershell-5.1

Format-Wide

Last, but not least the last command for today. It’s called Format-Wide and the name tells us what it does: Formatting something in a wide format.

Unbenannt.PNG

Microsoft Docs to Format-Wide:

https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Format-Wide?view=powershell-5.1

Review


Format-Table, Format-List and Format-Wide adjusts the output of a command. Use it with a pipe.


Format-Table, Format-List, Format-Wide in Advanced Mode

That’s not the end of this article. Now I’m going to show you some interesting and useful stuff about formatting. It’s a small collection of my most useful formatting parameters.

Format-Table -Autosize

The parameter adjusts the column size based on the width of the data.


Get-ChildItem C:\Temp | Format-Table -AutoSize

Unbenannt.PNG

Format-Table -Wrap

Displays text that exceeds the column width. By default, text that exceeds the column width is truncated.


Get-EventLog -LogName Security -Newest 2 | Format-Table -Wrap

Unbenannt.PNG

Format-Wide -Column

Specify the number of columns.


Get-ChildItem | Format-Wide -Column 3

Unbenannt.PNG

Some more examples … Active Directory (Side Note)

If you are working with Active Directory, you can also use the format commands to display users. Log in to a domain controller and start PowerShell. Here is some examples:


Get-AdUser administrator -Properties * | Format-Table CN,Enabled,LastLogonDate,SID -AutoSize -Wrap

1.JPG

Format-Table only makes sense with a few properties. Use Format-List to list more:


Get-ADUser administrator -Properties * | Format-List CN,Enabled,LastLogonDate,SID,Created,ProfilePath,ScriptPath,LockedOut

Unbenannt.JPG

Or do it for all users with the Filter * parameter.


Get-ADUser -Filter * -Properties * | Format-Table CN,Enabled,LastLogonDate,SID -AutoSize -Wrap

Unbenannt.JPG

Similarities with famous people from the film pumping iron are purely coincidental 😉

Exercise

That’s it for today. Here’s the exercise up to the next part.

Run Get-Hotfix. Show only the number (KB) of the hotfix and the install date in list format.

The solution can be found in the next part.

See you at the next article: PowerShell for Beginners (Part 8): The Power of PowerShell – Getting in Touch with Objects (Get-Member, Select-Object)


Patrick Gruenauer, MVP PowerShell

Categories: PowerShell

Tagged as: ,

3 replies »

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.