After we get an overview of what Powershell is, we are ready to get started. So let’s Get Started!!
Installation
Installing Powershell is extremely easy. Even better, it comes pre-installed with Windows 7 (Desktop Environment) and Windows Server 2008 R2 (Server Environment) onwards. It is available on the Microsoft website to be downloaded and installed in other versions of Windows XP,2003, and Vista.
Launching PowerShell
On Windows, you can launch PowerShell from the Start Menu. On non-Windows platforms, you can open PowerShell Core from the terminal.
Interactivity Is 'The Key'
Powershell REPL. Don’t worry about the terminologies. REPL means Read-Evaluate-Print-Loop. This simply means that it reads the value that you write, evaluates it, produces the result, and moves ahead to the next loop instance. Let’s try this:
We didn’t really write any complicated code or script, instead, we just wrote what we needed and it worked. Powershell is highly interactive. Let’s try another example.
See !! It simply works.
If you are familiar with .NET Framework (Don’t worry if you are not), you can also try something like this:
# Specify the path to the file you want to check
$fileToCheck = "C:\Path\To\Your\File.txt"
# Check if the file exists using the .NET System.IO.File class
if ([System.IO.File]::Exists($fileToCheck)) {
Write-Host "The file '$fileToCheck' exists."
} else {
Write-Host "The file '$fileToCheck' does not exist."
}
Yes, it is extremely flexible with that too.
Cmdlets: The Life Saver
The instructional commands in Powershell are known as CMDLETS (pronounced command-lets).
Each cmdlet has a VERB-NOUN format. For example Get-Help, Write-Host, Clear-Variable, Get-Content, Out-File, Get-ChildItem, and so on. And nothing to worry about cases. Powershell is not case-sensitive, not even in the case of variables.
Get-Help is the most handy cmdlet that you would like to keep under your belt.
If you need to know what are the cmdlets related to FILE. No worries, just type Get-Help File and you will get the list. The asterisk is for wildcard which means “anything”. So the File means <anything>File<anything>, which again essentially means “starts with any letter, word, symbol and ends with any letter, word, symbol but must contain File in-between.
Get-Help *File*
And if you already know the cmdlet, say Out-File, you can get the information about it with this:
If you need examples for Out-File cmdlet, you can type
Get-Help Out-File -Examples
If you need every detail about it including examples,
Get-Help Out-File -Detailed
Execution Policy
Powershell has a security feature called Execution Policy. This policy saves you from executing scripts from non-trusted sources. By default, Powershell will set itself to Restricted policy which means you cannot execute scripts.
To know what your Powershell environment Execution policy is set to, just type
Get-ExecutionPolicy
The Set-ExecutionPolicy cmdlet enables you to determine which Windows PowerShell scripts (if any) will be allowed to run on your computer. Windows PowerShell has four different execution policies:
Restricted – No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned – Only scripts signed by a trusted publisher can be run.
RemoteSigned – Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted – No restrictions; all Windows PowerShell scripts can be run.
We will go into detail about how to work with these in my later posts.
Happy scripting!