Introduction
Are you ready to level up your PowerShell skills? In this video, we’ll cover 10 essential PowerShell commands every IT pro, sysadmin, and developer should know. These commands will streamline your scripting, automation, and everyday tasks on Windows. Learning PowerShell is very important in order to become a better and more effecient system administrator. For example, what if you wanted to get the DNS name of 500 servers and you only have access to PowerShell? Are you going to use nslookup for each server or are you going to write a script that loops through each server and saves output to a file.
Disclaimer: I understand these are not all considered ‘commands’. The term ‘commands’ was simply used as a Title for the video.
PowerShell Commands
Get-Content
The Get-Content cmdlet is very useful when you need to read a file. A lot of the times this will be used to save text from a file as a variable and later loop through it to perform a certain function. For example, a list of IP addresses we control. We can use this list to be a central location to update addresses and then use the input from the list inside of a script to perform a ping to determine if host is online or if a particular port is open.
You can test this by saving a .txt file with some example IP addresses
8.8.8.8
192.168.1.1
192.168.1.2
1.1.1.1
Open PowerShell and make sure you change directory (cd) to the directory where this .txt file is stored. Then use gc <filename>.txt to read in the contents of the .txt file
gc .\ip.txt
192.168.1.2
192.168.1.1
8.8.8.8
1.1.1.1
Before covering the ForEach loop it will benefit us if we understood how to store the input from .txt file into a variable. This will then allow us to iterate through each line (ip address) in the .txt file.
All you need to do is create a variable and assign it by doing the following
$iplist = gc ip.txt
ForEach-Object
Similar to Python and other languages we have the ForEach loop. This will allow us to loop through an object to perform a certain task. In this case we will want to loop through the list of ip addresses and test if online. In the previous instruction we have learned how to assign the contents of the .txt file to $iplist variable.
All we need to do is the following
foreach ($ip in $iplist){write-host $ip}
foreach is an alias for ForEach-Object (we will discuss this later). Inside of the parentisis we break down each line into a new variable $ip. We then write this output to terminal by using write-output.
Test-NetConnection
So now that we have the ability to loop through the list of ip addresses we can now perform a test to see what hosts are online.
foreach ($ip in $iplist){Test-NetConnection -ComputerName $ip -port 22}
Using the Test-NetConnection cmdlet we will need to define the -ComputerName parameter along with the port. If port 22 is closed you will see TcpTestSucceeded : False
Out-File
Great so at this point we have performed a port scan and within the PowerShell terminal we can see the results. What if we wanted to view this at a later date? That is where Out-File can be useful. First we can go ahead and save the results to a variable similar to what we did in the very first step.
$results = foreach ($ip in $iplist){Test-NetConnection -ComputerName $ip -port 22}
We can then pipe the $results variable into Out-File followed by the filepath
$results | out-file results20241009.txt
Get-Date
Another important cmdlet that you will most deffinately use as a System Administrator is Get-Date. One of the very first scripts I created allowed me to find any inactive users and disable the user all by using Get-Date. This allowed me to compare todays date vs last login date and then determine if greater then a pre-defined amount of days.
Get-Date
Wednesday, October 9, 2024 3:38:21 PM
Using the format parameter we change it up to our liking
Get-Date -Format "MM/dd/yyyy"
10/09/2024
We can also use the Get-Date cmdlet to get the time
Get-Date -Format "yyyy-MM-dd HH:mm:ss"
2024-10-09 15:40:00
Start-Sleep
Start-Sleep cmdlet although is useful, I do not consider to be that important to learn. With that said their will be times where you will find yourself using it. Mostly this will be used in a script where you want to write something to terminal and allow time for the users to read the prompt.
Start-Sleep -Seconds 5
This will put the terminal to sleep for 5 seconds
A real world example would look something like this
Write-Host "Starting task..."
Start-Sleep -Seconds 2
Write-Host "Task in progress..."
Start-Sleep -Seconds 3
Write-Host "Task completed!"
Write-Host
Similar to Start-Sleep, Write-Host is not as important as the others but will be used a lot. Write-Host will allow you to write text to the console, weather that be for the administrator troubleshooting or for the user executing the script.
Write-Host "Hello, World!"
Maybe we want to write an exception or error to screen with red, that is where Write-Host will be useful
Write-Host "This is green text" -ForegroundColor red