PowerShell Pipeline

What I Like in PowerShell V5, Part 1: Cmdlets

Here are some of the best new cmdlets that you might be missing out on.

PowerShell V5 has been out for a while now and with that I wanted to take the time to show off a few things that I like and/or use regularly. Note that at the time me writing this, PowerShell 5.1 is currently available as a preview, but I will not be covering anything new that comes along with that version. When it goes out of a preview release, I will be sure to post my thoughts on that soon after. If you are currently missing PowerShell V5 and want to join in the fun, you can download it from here.

If you have been an avid reader of MCPMag.com, then you may have already seen some of the talk about PowerShell V5 in the way of the Archive cmdlets, the PSReadLine module for better command line use and the PowerShellGet cmdlets for working with the PowerShellGallery, to name a few. With PowerShell V5, the PowerShell team has really gone all out with providing some other great things that we will take a look at.

I'm going to focus part one of this two-part series on some of the cool new cmdlets which are now available to use as well as a familiar cmdlet with a great new feature!

Creating a GUID Quickly: The first cmdlet that I wanted to highlight is a simple, yet useful cmdlet to quickly create a GUID to use for your enjoyment.

 

New-Guid 

Guid                               
----                                
e3869592-5523-4bb7-9011-7c5832389c4

Nothing major here, just a quick way to create a random GUID.

Create a New Temporary File: Much like the previous cmdlet to create a GUID, we have a simple cmdlet to create a temporary file in your Temp folder on your profile. The file name is a random file that has no meaning and isn't meant to last forever. Just long enough to add what you need for the given moment.

New-TemporaryFile

    Directory: C:\Users\proxb\AppData\Local\Temp

 

Mode                LastWriteTime         Length Name                         
----                -------------         ------ ----                         
-a----        8/15/2016   7:02 PM              0 tmpC752.tmp 

Yes, the file does actually exist and is available to write anything you want to it knowing that it will not last forever as it is just that, a temporary file.

Enter the Clipboard Cmdlets: Something a little unexpected are the new cmdlets that support the clipboard. We get both a Get-Clipboard and Set-Clipboard in PowerShell V5.

[Click on image for larger view.] Figure 1. Looking at the clipboard cmdlets.

First let's take a look at the Get-Clipboard cmdlet and look at the syntax of the cmdlet along with its parameters.

[Click on image for larger view.] Figure 2. Syntax for Get-Clipboard.

The first that you might notice is the –Format parameter which accepts values ranging from text to image or audio. This means that we can view copied text or the object which represents an image or audio clip that was copied!

Get-Clipboard -Format  Image 

Tag                  :
PhysicalDimension    : {Width=919, Height=51}
Size                 : {Width=919, Height=51}
Width                : 919
Height               : 51
HorizontalResolution : 96
VerticalResolution   : 96
Flags                : 335888
RawFormat            : [ImageFormat: b96b3caa-0728-11d3-9d7b-0000f81ef32e]
PixelFormat          : Format32bppRgb
Palette              : System.Drawing.Imaging.ColorPalette
FrameDimensionsList  : {7462dc86-6180-4c7e-8e3f-ee7333a7a483}
PropertyIdList       : {}
PropertyItems        : {}

Pretty cool, right?  Personally, I assumed that this would only apply to text, but PowerShell once again goes the extra mile by giving you the object representation of something other than text.

In the same way we looked at Get-Clipboard, let us also take a look at Set-Clipboard and see its syntax.

[Click on image for larger view.] Figure 3. Syntax for Set-Clipboard.

Definitely a lot of things here that we can do with Set-Clipboard but one cool thing is that we can copy the contents of a folder by using the –Path parameter and providing a file path. Note that you have to specify a wildcard character to copy the items underneath the directory.

Set-Clipboard -Path "C:\Users\*" 

We can then use Get-Clipboard with the –Format parameter and supply a format of FileDropList to properly display the items underneath C:\Users.

Get-Clipboard -Format  FileDropList 

    Directory: C:\Users

Mode                LastWriteTime         Length Name                                                                               
----                -------------         ------ ----                                                                                
d-----        8/12/2016   7:40 PM                Default.migrated                                                                   
d-----        8/14/2016   7:40 PM                proxb                                                                               
d-r---        8/12/2016   7:34 PM                Public   

What you get back is the actual object that you can work with via .Net methods.

[Click on image for larger view.] Figure 4. Using Set-Clipboard for file objects.

Powering up Get-ChildItem with Depth: The last thing that I will show off today is what is new with Get-ChildItem. While it is a single parameter, it happens to be a parameter that has been sought after since PowerShell came out. While Get-ChildItem has the ability to recursively look into a file system and filter now by File or Directory , the one thing that was missing was a way to limit just out deep into a folder structure that the cmdlet should go. This meant that many of us had to add our own checks to provide a depth limit so we wouldn't waste resources looking for something.

With the Depth parameter, we can supply an integer value and Get-ChildItem will respect that value when you combined with –Recurse. No more writing our own functions to handle this!
Get-ChildItem -Recurse -Depth 1 -Directory

    Directory:  C:\Users\proxb\Desktop

 

Mode                LastWriteTime         Length Name                                                                               
----                -------------         ------ ----                                                                               
d-----        7/31/2016   9:28 AM                Art_Of_PowerShell_Runspaces                                                        
d-----        8/14/2016   6:24 PM                PoshRSJob                                                                          

 

    Directory: C:\Users\proxb\Desktop\PoshRSJob

 

Mode                LastWriteTime         Length Name                                                                                
----                -------------         ------ ----                                                                               
d-----        8/14/2016   6:24 PM                1.5.7.7                                                                             
d-----        8/14/2016   6:24 PM                1.6.0.0   

And with that, we are done with the first part of this series on PowerShell V5 things that I like. The next part in this series will continue my look at what I think is great with V5. Be sure to let me know your thoughts of what you like below in the comments.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.