Surviving as a PowerShell n00b. Learn PowerShell Basics, Faster.

Here are some disclaimers (to possibly save you some time):

Click to Hide / Acknowledge
  • I assume that you have some basic command line knowledge and programming skills.
  • I assume that you’re pretty new to using PowerShell.

    Basically, I want to provide a few PowerShell tips that I learned after my time of confusion. I hope that this saves you a few hours of trial and error.

  • I am definitely not a PowerShell expert (yet)… you read the title, right?
  • ! 57!11 n33d 4 845![ 1337 [0nv3r73r 700, 50 n0 w0rr!35. ! 4m 4 n008 700.
  • At the time of writing this, PowerShell 6.2.3 was the stable release.


I’ll try to condense the reasoning for why I needed to learn PowerShell, but I do believe there is benefit in knowing some context.


Or you can Skip To Takeaways


Lists are cool, dude.

Problem(s):

  • I have log data from multiple devices that need to be compiled into one single CSV file.
  • I have a proprietary tool for each device capable of producing separate CSV files, some existing batch scripts for moving, merging, and cleaning up files, and a C# application that I created myself for inserting an identity column into existing CSVs.
  • I want to learn PowerShell (Could be a problem).

Solution Approach:

  • I could build on existing code to solve the main problem, but I decided to learn PowerShell to avoid having to touch existing batch files or version out any more C# applications. This gives me more flexibility for any future feature updates. For example, if I were to deploy on a server environment, a C# development environment would not be required to make changes. Have I mentioned that I just want to learn PowerShell? lol
  • I believe to make this work, I’ll need three critical pieces of knowledge
    – How to create a generic function for repeated steps.
    – How to reference files relative to the script’s location.
    – How to start and stop programs.
    If I fail to address any of these critical pieces in my takeaways, please let me know.

Takeaways

1) Edit and Debug PowerShell Scripts with the ISE (Integrated Scripting Environment)!

Full disclosure: I wasted my first hour of learning PowerShell by debugging through a series of print statements (or ‘Write-Host’ commands) before realizing this nice tool existed for writing and debugging scripts!

2) Look through Snippets before trying Google!

Not sure about the syntax of a certain loop or how to define a function? Snippets can help!

‘Start Snippets’ can be accessed by pressing Ctrl + J or in the Edit menu click Start Snippets.

The hover/tool-tip description is usually enough for me to get started without having to Google an example.

3) How do I reference files or folders relative to the PowerShell script’s location?

$CURRENT_SCRIPT_PATH = split-path -parent $MyInvocation.MyCommand.Definition
Explanation

I believe this is true for most scripting, but if you call a PowerShell script located outside of your current working directory, then local references in the script will look in your current working directory and not the directory of the script’s location.

This line will create a variable called “CURRENT_SCRIPT_PATH” which you can use to then reference files relative to the script’s location.

4) | The Pipe is Your Friend |

The use case I most frequently find myself in is when I need to do the same modification to multiple files.

I can pipe things like the outputs of ‘ls’ or ‘echo’ commands as inputs into other functions.

For example:

# Convert .txt to .csv
ls *.txt | Rename-Item -NewName { $_.Name -replace ".txt",".csv" }

# Replace spaces with underscores.
ls *.csv | Rename-Item -NewName { $_.Name -replace " ","_" }

5) Add a Pause for Status Updates

# Status Report
Write-Host "Task 1 Complete. Starting Task 2"

# Wait 5 Seconds...
Start-Sleep -s 5
Explanation

This is nice if you want a quick way to track when a certain portion of the script is complete. You can deliberately add a pause with ‘Start-Sleep’ and can specify the units and amount.

6) Start a Process and Wait for Completion

$addColumnProc = Start-Process $ExePath -ArgumentList $CSVFiles

$addColumnProc.WaitForExit()    
Explanation

We define the variable ‘$addColumnProc’ to store the Process object containing the ‘WaitForExit’ function.

7) Defining and Calling a Function

function Update-CSV-IDs
{
    Param ($ExePath, $CSVFiles)

        $addColumnProc = Start-Process $ExePath -ArgumentList $CSVFiles

        $addColumnProc.WaitForExit()      
}

$CSVNameArray = ls *.csv

Update-CSV-IDs -ExePath $currentDir\Program.exe -CSVFiles $CSVNameArray
Explanation

Notice when calling the function, the parameter is specified before the argument that will be passed in. Also, assume that ‘$currentDir’ is previously defined.

8) If All Else Fails, Microsoft Has Good Documentation

All seriousness, I’m not trying to be a smart ass.

If you come across an example with a function you’ve never heard of, try Googling “<the name of that mystery function> + MSDN” or “<the name of that mystery function> + Microsoft docs”.

I’ve done this many times to find out exactly what is returned by some functions and for some official examples published by Microsoft.

I usually find myself seeing the name of a command that, in my head, sounds related to the task I need to accomplish, but I have no idea how it is used. The Microsoft documentation usually points me in the right direction and at the very least helps me come up with something better to look for when searching the rest of the internet.


I hope I’ve provided some useful knowledge in the early stages of your quest to learn PowerShell. Please feel free to share how you’ve used PowerShell or any issues you’ve found with my information.