PowerShell as Your First Programming Language

In this post, I discuss why PowerShell makes a great first programming language
PowerShell as Your First Programming Language
In: PowerShell, PowerShell 101, Code

Why PowerShell Is a Great First Programming Language

  1. If you are a Windows user, PowerShell is immediately available for you to use, no additional installation required
  2. If you are not a Windows user, PowerShell runs on Linux and Mac OS and installation is easy
  3. It is a very flexible programming language and easy for beginners to use
  4. It is an Object-Oriented Programming (OOP) language
  5. You can easily transfer the skills you learn from PowerShell to other programming languages

Isn't PowerShell just for sysadmins?

Sure, that's one of the most common uses of PowerShell, but you can write scripts to do much more than just system administration. For example, you could write a script to interact with an API, generate some reporting details, and email you the output. The options are unlimited.

And, if you are into penetration testing and ethical hacking – like I am – PowerShell is an extremely important language to know during assessments.





A Quick Note on PowerShell Versions

Just like Python – and other programming languages – PowerShell has gone through several version increments. Just as there are Python 2 and Python 3, there are different versions of PowerShell too.

PowerShell 5 and Below

On Windows 7 through Windows 11, you are going to have PowerShell 5 installed out of the box. There is literally no further installation necessary to get started.

PowerShell 6+ (PowerShell Core)

This version will run on major operating systems – Windows, Linux, and Mac OS. Regardless of your operating system, if you want the latest version of PowerShell, you must install it.





How Do I Get Started With PowerShell?

Windows

I strongly recommend installing the Windows Terminal app. In the Windows Terminal app, you can open multiple tabs and customize it with different colors and backgrounds; as well as other options.

Get Windows Terminal - Microsoft Store
Download this app from Microsoft Store for Windows 10. See screenshots, read the latest customer reviews, and compare ratings for Windows Terminal.
  1. Go to the Start Menu
  2. Type Windows Terminal and open it

If you call the environment variable $PSVersionTable, it will some output information about your current version of PowerShell.

Linux

I am going to use my Kali Linux VM as the Linux example.

Installing PowerShell on Kali Linux

Kali Linux is based on Debian Linux, so you can follow the instructions in the Microsoft documentation for Debian. Just follow the steps under: Installation on Debian 10 via Package Repository

Installing PowerShell on Debian Linux - PowerShell
Information about installing PowerShell on Debian Linux

Launching PowerShell

If you call the environment variable $PSVersionTable, it will output some information about your current version of PowerShell.





Some Basic Commands

The nice thing about PowerShell is that if you are used to running commands in Unix-style shell, many of the commands will work in PowerShell because the developers have added some aliases to make it work.

PowerShell Cmdlet Anatomy

In PowerShell, the commands that you run are called cmdlets. Typically, cmdlets do one thing really well. The idea is to have multiple cmdlets that all do one thing really well and chain them together in a "pipeline".

PowerShell cmdlet names should conform to a standard – Verb-Noun. The idea behind this is that a cmdlet should be very clear about what it does. This is important, so that the user can be sure of the nature of the cmdlet.

For example, a cmdlet that starts with a Get- verb, you can be confident that this is not a destructive cmdlet. However, a cmdlet that starts with the verb Remove-, you can be sure that this is a destructive cmdlet and should be very careful with how you use it.

You can view a list of PowerShell approved verbs here:

Approved Verbs for PowerShell Commands - PowerShell
Approved Verbs for PowerShell Commands



Aliases

An alias is just a convenient way to say run a command using another name. For example, in Linux, the way you list files and folders is with the ls command. In PowerShell, you run Get-ChildItem. Let's take a look at some aliases for the Get-ChildItem command.

I am back on my Windows Terminal and issued the command Get-Alias -Definition Get-ChildItem. In other words, get me an alias that points to Get-ChildItem. So, any time I run dir, gci, or ls, I am actually running Get-ChildItem.



Get-Help

  • Get-Help cmdlet-name



Get-Location

  • Aliases: pwd, gl
  • This command tells you what your current working directory is.
    Think of it as though you're looking at your file explorer.
    Which folder do you have open now?



Set-Location

  • Aliases: cd, chdir, sl
  • This command changes the working directory.
    Think of your file explorer again. Which folder do you want to open?
    • Example: cd C:\Windows\System32
      • Now you are working in the shell from this directory.



Get-Item

  • Aliases: gi
  • This command retrieves a single item from the operating system
    Not in the same sense as double-clicking a file.
    You'll just see the file or directory's metadata.
    • Example: Get-Item ~/Desktop/myfile.txt
    • This will return details about the item to stdout
      • Note: the ~ symbol in the path just means "the current logged in user (which is you)"



Get-ChildItem

  • Aliases: ls, dir, gci
  • This command retrieves any and all items in a specified path
    Think of a file explorer. When you open a folder, you see a list of all files and folders.
    • Example: ls ~/Desktop
      • This will return a list of all files and directories in the current user's desktop directory to stdout



New-Item

  • Aliases: ni
  • Think of a file explorer. You are creating a new file or folder.
    • Example: New-Item -ItemType File -Path ~/Desktop/emptyfile.txt
      • This command will create a file called emptyfile.txt in the current user's desktop directory
    • Example: New-Item -ItemType Directory -Path ~/Desktop/EmptyFolder
      • This command will create a directory called EmptyFolder in the current user's desktop directory



Remove-Item

  • Aliases: del, rm, rmdir, erase, rd, ri
  • This would be the same as deleting a file or folder in your file explorer.
    • Example: Remove-Item -Path ~/Desktop/emptyfile.txt
      • This command will delete the file called emptyfile.txt from the current user's desktop
    • Exmaple: Remove-Item -Path ~/Desktop/EmptyDirectory -Recurse
      • This command will delete the directory EmptyDirectory from the current user's desktop
      • We specify -Recurse to say if the directory has contents, delete everything within it.



Get-Content

  • Aliases: cat, type, gc
  • Example: Get-Content ~/Desktop/myfile.txt
    • This command will read the file and write its contents to stdout



Out-File

  • Example1: 'I want to add this string and overwrite everything' | Out-File ~/Desktop/myfile.txt
    • You're seeing the first of example of a pipe '|'
    • We write the string and send the stdout down the pipe to the command.
    • Out-File then receives the stdin and writes the contents to the file, overwriting everything.
      • Note: Not all commands accept pipeline input.
  • Example2: 'I want to append this text to the end of the file' | Out-File ~/Desktop/myfile.txt -Append
    • The stdout is passed down the pipeline to Out-File's stdin.
    • Out-File "appends" the stdin to the end of the file in addition to existing content.
  • Example3: 'I want to overwrite everything in the file again' > ~/Desktop/myfile.txt
    • Does the same thing as Example1
    • The > character means redirect stdout and overwrite everything with this text.
  • Example4: 'I want to add this text to the end of the file again' >> ~/Desktop/myfile.txt
    • Does the same thing as Example2
    • The >> characters means redirect stdout to the end of the file in addition to existing content.
  • Example 5: cat ~/Desktop/File1.txt > ~/Desktop/newfile.txt
    • Recall, cat is an alias for Get-Content
    • So, we're reading the file and redirecting stdout to newfile.txt
    • So yes, you can combine commands together like this.



Get-Command

  • Aliases: gcm
  • Example 1: Get-Command returns a list of all commands available to the current PowerShell session
  • Example 2: Get-Command Get-* returns a list of all commands starting with Get- in the current PowerShell session





Learning to Code with PowerShell

Once you're comfortable with the concepts here, move on to the next post in this series and learn the fundamentals of coding using PowerShell.

An Intro to Programming with PowerShell
In this post, I go over the fundamentals of programming using PowerShell and demonstrate why it’s a great way to learn to code

More from 0xBEN
Table of Contents
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to 0xBEN.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.