Here are some disclaimers (to possibly save you some time):
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
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
6) Start a Process and Wait for Completion
$addColumnProc = Start-Process $ExePath -ArgumentList $CSVFiles $addColumnProc.WaitForExit()
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
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.