PowerShell Bits And Pieces

I recently came across a few interesting things in PowerShell and I’m writing it down for future me or anybody else that might find this useful.

Importing CSV files
If you have a CSV file with proper headings in the first row you can import it using the Import-CSV command and you will end up with an array of objects that contain properties matching the headings. For example:

Name, IP, Role
 Srvr1,10.0.0.1,Data
 Srvr2,10.0.0.2,Web

Will give you an array with the following:

CsvArray

Using Visual Studio Code as your PowerShell IDE
Turns out VS code is actually very handy when editing PowerShell, you get intellisense, debug, validation, watches and key bindings that closely match Visual Studio to name but a few features. I used Visual Studio before VS Code as my IDE and although it worked very well, VS Code is lightweight and less cluttered and only takes few seconds to load even on a low end Windows tablet. You have to download the PowerShell plugin and enable but luckily that can now be done from the GUI instead of the command line.

InstallPS

You have to do a bit of work to get debugging to work, click the debug icon, then the gear icon to edit the launch.json file. For every script you want to debug you have to create a new entry and specify the name of the PowerShell file and a meaningful name to display in the drop down list.

VSDebug

Break and ForEach loops
Just like C# you can use Continue to go to the next iteration of a loop and Break to exit the loop but the behaviour is different depending on where you use it. Break will exit the current loop if used in a Foreach, For, While, or Do loop and it will exit the current code block if used in a switch statement. Using Break outside a loop or switch statement will exit the script even if it is used inside a code block not the main script. When you use Break inside a ForEach command Break behaves as expected and will exit the immediate loop but if you are using the ForEach-Object cmdlet Break will not exit the loop it will exit the script.

PowerShell without remoting
There are a few PowerShell commands that you can run remotely without using PowerShell remoting, PowerShell must be installed on the remote computer but PowerShell Remoting doesn’t have to be enabled or configured. You can find the complete list is here. I found this feature very useful to execute WMI queries for inventory purposes.

Working With Regex
Regex behaves like it did in .NET you use the Select-String cmdlet and it returns a MatchInfo object for each match. For example to find lines in a file that start with a UTC date.

$match = Get-Content c:\temp\logfile.txt | Select-string -Pattern '^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d).(\d\d\d)Z' | Select-Object Line

The Line property will show the whole line in the file that matched while the  Matches property shows more information around the pattern that was matched. You can also use Select-String to process multiple files in a directory by specifying a filter and the MatchInfo object for each match will contain information about the file, line no etc where it was matched.

Direct Output In Two Directions
You can use Tee-Object command to redirect output to a file or variable and still pass it to the pipeline as well. For example to keep the output of a process in a log file but still see what is happening in the console and even process the output further down the pipeline:

Start-Process ... | Tee-Object C:\Output.log | ... more processing ...

Francois Delport

Published by

Francois Delport

I am a cloud and devops consultant, technology fan and previously a professional C# developer with a keen interest in system design and architecture. Currently I am involved in projects using Azure, the Microsoft stack and DevOps. I am based in Melbourne, Australia. Email: [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *