PowerShell primers

PowerShell for beginners: Scripts and loops

PowerShell primers

Show More
1 2 3 4 5 6 Page 2
Page 2 of 6

An introduction to scripts

The basis of PowerShell is script processes and commands. Once we have the framework of creating our own scripts down, we can add in some of the more advanced logic that involves loops and conditionals.

Scripts in PowerShell are basically just text files with the special filename extension of ps1. To create a script, you enter a bunch of PowerShell commands in a sequence in a new Notepad file (or really, you could use any text editor you like), and then save that file as NAME.ps1, where NAME is a friendly description of your script -- with no spaces, of course.

To run a PowerShell script that you already have, you enter in a PowerShell window either:

    • The full path (folder and file name) of the script, like so: c:\powershell\myscripthere.ps1

Or

  • If your script is in the current directory the console is looking at, use a period and then a backslash, like this: .\myscripthere.ps1

Other than that, there is nothing special needed for creating a script in PowerShell. You simply add the commands you like.

Of course, a script probably needs to do more than one thing or you wouldn’t need a script for it. Scripts are common in IT already; administrators have been using login scripts for years to get users' desktops and environments configured consistently every time a user logs on. As technology has gotten better, you can script almost anything, from the bare-metal installation of an operating system on a server fresh out of the factory box all the way up to server workloads, including installing Exchange or file server roles.

We obviously won't go that in depth in this piece, but the basic idea behind a script for our purposes is to do two or three things and then end.

To do that, we need to cover a few elements of a script. The first is the element that can change. The second is the element of the script that actually does the dirty work on everything. Let's look at each of the phases.

1 2 3 4 5 6 Page 2
Page 2 of 6
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon