Categories
PowerShell Basic

PowerShell the absolute basics

command-line shell

PowerShell is a command-line shell scripting language that runs various commands in the terminal or creates more complex scripts to execute.


In 2006, PowerShell was introduced into the Windows operating system and has become a very popular tool for automating Windows tasks. Although PowerShell focuses on Windows administration, it can now be installed on Mac and Linux.


To run PowerShell, you can start the Windows PowerShell ISE or just start the Windows PowerShell command prompt. If you only want to run commands, it is easier to open the command prompt. But for creating scripts, PowerShell recommends ISE or Visual Studio Code.

Let’s start using some basic commands:

Write-Host "My PowerShell Journey Starts Here!"

What this command does is it writes back to the terminal so the output is: My PowerShell Journey Starts Here!


There is one more command that does much the same thing only it is used to print to the pipeline this means it is used so that the next command can use it as its input.


Ex:

Write-Host "My PowerShell Journey Starts Here!"
Write-Output "Or here"
Write-Output 1,2,3,5 | Measure-Object # PowerShell object

CmdLets

Commands in PowerShell are called CmdLets and are based on “Verb”-“Noun” as in the example above “Measure”-“Object” or “Get”-“Date”.

Ex:

Get-Date

These examples are just a few of the many CmdLets that you can run in the PowerShell terminal. To list the available commands you can run the command Get-Command.


Ex:

Get-Command

To list only verb commands
Ex:

Get-Command -Verb Get

Or to list all Nouns equal to Host


Ex:

Get-Command -Noun Host

If you read through the entire list, you will find a command called Clear-Host, which clears the terminal. This is a widely used command because it easily becomes a lot of text in the terminal and it’s nice to get rid of it.
Ex:

Clear-Host


Alias
There are also aliases that you can use and we will get into that here with Clear-Host as the alias for that is cls.


To list all pre-created aliases, type Get-Alias
Ex:

Get-Alias

To list the various commands Alias has, you can run Get-Command -Noun Alias.
Ex:

Get-Command -Noun Alias

PowerShell includes the following cmdlets, which are designed for working with aliases:

  • Get-Alias – Gets all the aliases in the current session.
  • New-Alias – Creates a new alias.
  • Set-Alias – Creates or changes an alias.
  • Remove-Alias – Deletes an alias.
  • Export-Alias – Exports one or more aliases to a file.
  • Import-Alias – Imports an alias file into PowerShell.

To create a new alias, run New-Alias or Set-Alias (This will overwrite the existing one if it already exists).
Ex:

New-Alias -Name al -Value Get-Command
al -Noun Alias

In this example we used two parameters Name and Value.

  • Name tells what name the alias should have and
  • Value tells what value (Command) the alias should have.

So when we now run al -Noun Alias we get the same result as running Get-Command -Noun Alias.
Now this alias will only be created for this session. You can choose to export them to a file and then import it every time you start a new session. In order to save them permanently and for them to load for the user at each new start, you must create a profile.ps1 file under powershell’s profile.

The easiest way to find out where it is is by running echo $profile in the terminal to bring up the user’s profile directory. Under this path you create profile.ps1 if it does not already exist and you can enter your aliases in it.
Ex:

echo $profile

Will return the path in which to create the file:
Ex:
C:\Users\\Documents


Create the file profile.ps1 and in it create aliases New-Alias -Name zone -Value Get-TimeZone


Ex:

New-Alias zone Get-TimeZone

Chose an alias of z because it comes late in the alphabet and when you run Get-Alias, they are listed in that order. When you have saved profile.ps1, you can start the terminal again and run the alias. Or find it up:

Get-Alias

There is a lot more to aliases so have mostly gone through the usual way of dealing with aliases. The neatest thing would have been to create an export of the alias that you create and then import it into profile.ps1.
It works just fine, but you have to do something a little differently because by choosing Export, you export all active aliases, then if you choose to import it, you get a lot of errors that say that alias already exists, etc.
What you do is create an alias file with the file extension .csv or .ps1 (It is possible to create a normal text file as well, but not as nicely).
Then in your profile.ps1 file you import the alias.ps1 file

Ex:

Import-Alias -Path C:\PowerShellTheGame\Alias\alias.ps1
Alias.ps1

Alias File

Exported by : tempcoder

Date/Time : April 3, 2023 12:02:47

Computer : tempcoder

"s","Get-Service","","None"

What you import is a comma separated list “s” = Alias the name, “Get-Service” = Command “None” = Option
-Option

  • None: The alias has no restrictions (default value)
  • ReadOnly: The alias can be removed but cannot be changed except by using the Force parameter
  • Constant: The alias cannot be removed or changed
  • Private: The alias is only available in the current scope
  • AllScope: The alias is copied to all new scopes that are created
  • Unspecified: The option has not been specified.

In conclusion

One of the best commands is help. Get-Help alias help with that command you get all the information you need for all commands that exist.
I usually use it with the ShowWindow option, then it opens in a separate window and you have easy access to the help information.
Ex:

Help Get-Service -ShowWindow

That was the absolute basics of PowerShell.

If this was interesting please support me

Leave a Reply

Your email address will not be published. Required fields are marked *