Fundamental powershell cmdlets for beginners
October 13th, 2019
Powershell can be confusing for beginners, especially those coming from imperitive, compiled languages like C++ or C#. If you are not used to the command line, not only could you struggle with the syntax of powershell, but also the fundamental question of what you even do with powershell.
Powershell is often advertised as a great tool for network admins who have large clusters of machines to monitor and maintain. Remote computer administration tasks are advanced uses. In my opinion, Powershell is great for beginners (and beyond) who just want to explore and understand their own machine.
In this post I'll explore the top 4 cmdlets a beginner should know when getting started with powershell. You'll end up using these commands even when your an advanced powershell user. Ok, enough said. Lets explore some commands.
On the surface, the Get-ChildItem cmdlet is essentially getting all the items in a container (e.g. all the files and folders inside your current working directory). This is roughly equivalent to the "dir" command in CMD. In fact powershell automatically aliases dir to Get-ChildItem.
The basic usage will get you the list of files and folders in your current working directory along with some meta information about those items including the length in bytes if the item is a file.
PS /Users/Taylor> Get-ChildItem Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2/22/19 10:19 PM Alpha d----- 9/9/19 2:56 PM Beta d----- 9/24/19 9:23 AM 1028 gamma.txt d----- 7/1/19 11:58 AM 2034 delta.txt
You can pass the -Path flag to Get-ChildItem to explore containers outside of your current working directory.
Get-ChildItem -Path ~/path/to/folder
Other notable flags for the Get-ChildItem cmdlet are:
Get all the items in a container and all the items in all sub-containers.
- -Filter [pattern]
Only those items matching the pattern are returned. Can use * and ? wildcards.
Get items that otherwise wouldn't be returned such as hidden files. Doesn't override security restrictions.
Get-Content does exactly what it looks like. It retrieves the contents of files. It can do a bit more than that, but for a beginner that is what you'll use it for. In CMD the "type" command will print to the scren that contents of a file. Get-Content can print content to the screen, but often you will want to pipe the contents of a file to another cmdlet.
Lets look at some usages:
PS /Users/Taylor> Get-Content -Path hello.txt Hello, World!
PS /Users/Taylor> Get-Content -Path ~/Logs/ -Filter *2019*.log [Lots of output here]
Other notable flags for the Get-Content cmdlet are:
By default, Get-Content returns an array of strings, one string per line in the file. Using the -Raw flag you can get the entire contents back as a single string.
How many lines to read from a file from the start. Useful for checking if the file is what you are looking for.
How many lines from the end to read. Very useful for peeking at recent entries in a log file.
Select-Object takes an object and returns an object, usually after a bit of transformation. You could take in an array of DirectoryInfo object (produced by Get-ChildItem) and return another array of objects with just "Name" and "Extension" properties. You can also select the first or last item in a collection. Select-Object has a lot of in common with select in linq from C#.
Imagine you want to use the Get-ChildItem cmdlet to get all the files in the current directory. You would use this command:
PS /Users/Taylor> Get-ChildItem -File
Instead of getting all the properties back from this command image you just wanted the Name and Length property. You would pipe that output of that command to a Select-Object with the -Property flag:
PS /Users/Taylor> Get-ChildItem -File | ` Select-Object -Property Name, Length
The -Property parameter specifies which properties from the objects being piped in you want to extract and add to the objects coming out. Also notice you can use the backtick (`) to split commands across lines.
If you want to get the first item in a list use the -First flag.
PS /Users/Taylor> Get-ChildItem -File | ` Select-Object -First 1
Select has a lot of power, and you will use it a lot to transform the output of one cmdlet into the right "shape" for another cmdlet. Other notable parameters for Select-Object are:
- -Last [number]
Get the last X items in a list.
If objects have the same properties and values then only 1 will be added to the output collection.
- -Skip [number]
Skip X items from the start of a collection and return the rest.
- -ExpandProperty [property name]
Return a collection of a specificed property, rather than a collection of objects with that property.
- -Last [number]
Where-Object is your general-purpose filter: given a collection, filter the objects so that only those meeting a certain condition are returned as output. The simple usage is:
Where-Object Name -eq file.txt
This command is saying only return objects whose name property equals "file.txt". The -eq operator is the powershell equals operator (e.g. == in many other languages).
Where-Object has a lot of power, and is very useful for trimming down the output of one cmdlet before inputing it into another.
A couple of other notable parameters are:
If a property value is like rather than equals a certain value. Use wildcards * and ? to match.
Where-Object Name -Like *.txt
If a property value is not a value.
Powershell is a great tool for exploring and working with your own machine. If you are going to learn powershell I highly recommend focusing on the fundamentals first, because these commands and a few others are really the bread and butter of powershell. Happy scripting!