New PowerShell cmdlets in 5.0

Today I attended my first meeting of the Dutch PowerShell User Group (or DuPSUG for short) which contained an enormous amount of info on several topics. In this post I’ll discuss 3 of the PowerShell cmdlets I picked up of which I never even knew the existence. These cmdlets came from the session of Ed Wilson, “Ed’s 5 favorite things about PowerShell 5.0”. If you didn’t know he’s the guy behind the scripting guy blog. Thanks for the input Ed!

Compress/Expand-Archive

Handling zip archives was always a pain in the ass. You’d either have to resort to .net. Which I personally never used because the command line 7-zip was just so much easier. Then you would have to bundle your script with additional files or pull them down from the internet on-demand. Which creates the requirement for a functioning internet connection. Both options really never were that great. Luckily since PowerShell 5.0 we have Compress-Archive and Expand-Archive. I guess the names say it all. From now on we can use these cmdlets to zip and unzip files. It still isn’t perfect, you can’t pick specific files out of the zip file and there’s a 2GB limit but if you simply want to zip and unzip entire files/folders smaller then 2GB this is a great alternative to 7-zip or .net (actually both cmdlets directly use the .net libraries so you’re using .net either way).

Compress-Archive has a pretty simple syntax. In it’s most basic form it only needs a source file or folder (-path parameter) and a destination filename (-DestinationPath parameter). Like this:

Additionally you can add the -CompressionLevel parameter with the options “NoCompression”, “Fastest” or “Optimal”. Which either doesn’t compress at all (NoCompression). Compresses a bit (Fastest) or compresses as much as possible (Optimal).

You can also specify the -Update parameter. Which allows you to add files to existing archives.

Expand-Archive is even simpler then that. You have a -Path (for the source zip file) and a -DestinationPath (for the path where you’d like it to leave the files) parameter like with Compress-Archive.

Convert-String

Back in the old days (like half a year ago xD) before PoSh 5.0 was released when you wanted to do some serious string manipulation you needed regular expressions. Which are not the easiest thing to master. Convert-String really simplifies reformatting an array of strings. You can provide it with an array of string for it to manipulate. As input give it the current format and desired format of a single item in the array and hopefully it’ll be able to figure out what you wanted for the entire array.

To give you an example I’ve loaded everything from my Music library that starts with the letter “V” into the $Music variable:

I can then run Convert-String on it using the -example parameter. Within the parameter I have an original value and an example of how I want the new values to be formatted. See anything out of the ordinary?

If you’ve been paying attention you’ll see the biggest flaw.It forgot the “Van Halen – 1984” album entry (album name and the year it was produced is the same). If it doesn’t know what to do with a value it’ll just skip it without any warning or message. So if you’re using Convert-String always make sure you still have the same amount of values (using Measure-Object for example) before and after.

I felt that the first command only processing the artists with the letter “V” was quite slow. After measuring it using Measure-Command I found out it was a little over 2,5 seconds. $Music holds 9 albums/entries.

Luckily processing my entire music library is hardly slower. $AllMusic holds 148 albums/entries in this case.

Leave a Reply