The basics of PowerShell scripting: Your guide to becoming a PowerShell pro
PowerShell is an extremely powerful tool that, with the help of scripting, can automate almost anything you need to do on your computer or network. Whether you’re a Windows expert or just getting started, this beginner guide to PowerShell scripting can help you learn the basics so you can start automating all the things!
What is Windows PowerShell?
Windows PowerShell is a powerful, flexible, and easy-to-use scripting language. It’s primarily used for Windows, but it also has some cross-platform functions. As Microsoft puts it on their site, PowerShell simplifies the task of administering your system. But what does that mean? It means that you can automate tasks on one or more machines with scripts to save time and reduce errors. You can even use it interactively to run commands right away in the console!
Windows PowerShell is an object-oriented programming language which features tools that make system administration less difficult when compared with other languages such as C# or Java.
What You Need
This post is for those who are just starting out with PowerShell, as well as those with some experience. If you need to know the essentials on how to get started in this widely used system and writing scripts, you’ve come to the right place.
At the time of this post, PowerShell Core (included with Windows 10) is the latest version of this powerful tool.
With that being said, let’s take a look at the fundamentals of scriptwriting in general. Every good writer has his or her process, and that is where we will start. The process I use starts by coming up with an idea for a program or script that I want to create and all it does.
Installing Windows PowerShell
First, navigate to Microsoft’s download page and then select the option for Windows 10 64-bit (KB2919442) from the dropdown menu. You’ll need to wait for the file to download and then run it. Next, when prompted, you should agree with all of the changes that will be made by clicking I Agree.
After clicking Install, you will be given an option for installing PowerShell as well as for allowing non-Microsoft programs like Notepad++ and Firefox access. If you’re using one of these programs, allow them access; otherwise, simply click Next in order until it finishes installing.
Try Out Some Commands
In this tutorial, we will cover the basics of PowerShell scripting and show you how you can use it to automate tasks. Let’s get started!
You’ll want to first open up your terminal or cmd prompt, or whatever app is installed on your computer for executing commands in the console. Once there, let’s create a folder where we’ll be doing our work. Remembering that Powershell files are ‘just’ .ps1 files, so let’s just name this as scripts with no extension and put it in my Documents folder as seen below:
> mkdir scripts (make directory)
We’re going to need some kind of file for our script; any text file will do for now.
Writing Scripts
To create a new script, you can open Windows Power Shell (command window) and navigate to the directory where your new script will live on your computer, then type New-Item NameOfYourScript.ps1 or write $your_script_name = Write ‘PowerShell Script’ Here in Notepad++ or any other text editor and save it as .ps1 file. Once saved, open the file with PowerShell by typing Powershell in the command prompt (Windows Powershell/terminal). In PowerShell, you need to run Set-ExecutionPolicy RemoteSigned first so that unsigned scripts are allowed to run from anywhere on your machine. Next, copy-paste the following code into the window and press enter to execute it
For example, if you want your script to ask for a username at launch (and only prompt for a username), add param( [string]$username ) between #region Func and #endregion Func. If you wanted an Are You Sure confirmation box before every action taken by your script, add Write-Host -ForegroundColor Red Are You Sure? before everything else inside Initialize-SCript().
Example Commands – Second Paragraph: The final example script below has been written after reading through all these articles.
Breaking Down More Advanced Commands
In this section, we’re going to look at some more complex commands. For all of these, you’ll need to type the code for them yourself (instead of copy and pasting it). To execute your code, type Start-Process powershell. We’re also going to be using our PowerShell script in the next example. We’ll keep adding onto that script until we have something functional.
If ($IsAdmin) { Get-ChildItem -Recurse } Else { Try { Get-ChildItem -Recurse } Catch { Write-Error Access denied } } #End If #Get remote computers running certain programs function getComputerNames(){ $computers = @() $programs = @(PowerShell, wscript) foreach($program in $programs){ foreach($computer in get-content c:\scripts\psscript.txt){ if($computer -match $program){$computers += $computer} } } return $computers } #end function if ($args -eq C){ Connect-RemoteServer C }}
Tips & Tricks
PowerShell is a powerful tool that has been designed by Microsoft with developers in mind. It’s typically not easy, but there are plenty of resources out there that can help you, even if you don’t know the language. Start by learning what PowerShell does best – it is not a replacement for your current shell or text editor and should not be used as such. You might be tempted to use it as an IDE, but avoid this temptation at all costs because it will quickly become bloated and difficult to navigate.
Further Learning Resources
You’ve installed PowerShell and dabbled with a few scripts, but how can you take your skills to the next level? We’ll be tackling the fundamentals. In future articles we’ll discuss some more advanced topics like modifying the registry and creating graphical user interfaces. For now though, let’s get started! Scripts are simply pieces of text that have been saved as files (usually with the extension .ps1). There are two types of scripts in PowerShell, those that execute immediately when they’re run (e.g., running myscript from within PowerShell) and those that don’t (e.g., myscript is saved in a folder and then run by double-clicking on it). The latter type is called a module. To make this distinction clearer, modules will typically have an extension of .psm1 or .psd1 – however not all modules use these extensions so be sure to check before running them.