How to create a script in Powershell?

PowerShell has been around for a long time. It is a command-line shell and scripting language developed by Microsoft for task automation and configuration management. This article will show you how to create the first PowerShell script.

To do this, I will launch Visual Studio Code, which is the preferred PowerShell script writer at this point. Open it, and click on File > New Text File to create an Untitled file. Click on File > Save As to give that file a specific name. And notice, it tries to save it as plain text. So in the Save as type dropdown, I scroll down and choose PowerShell [1]. And I’m going to call it My First Script [2]. Now, we have a PowerShell script file.

When we looked at the editor, in the bottom section is Terminal. Any commands that I run here can be executed at the bottom.

Terminal section

Let’s try a couple of different approaches. The first one here is to create a script that would just return a value.

So, I’m going to create a $variable and set that to a specific value. When I’m using Visual Studio Code because I have the add-in for PowerShell, it’s now going to give me some warning/error type handling or at least tell me when things are not right.

Visual Studio Code suggest me to install Powershell extension
Visual Studio Code suggests I install the PowerShell extension

Hover the mouse over the $variable; you can see my variable isn’t being used by anything.

Warning: The variable ‘variable’ is assigned but never used.

If I hover my mouse over the equal sign, it will say, well, you must complete this.

Error: You must provide a value expression following the ‘=’ operator

Honestly, it can be a little bit confusing doing this because it starts to warn all over the place. But once we start typing in commands:

$variable = Get-Service

You’ll see the equals error has now gone away because we’ve created a variable and populated the right-hand side of it.

What I’m now going to do is utilize another method called Write-Host, Write-Host is a PowerShell print command. Now, notice what happens here.

Write-Host $variable

The warnings have now been removed because it’s continually checking to ensure that the values are correct, that the variables have values, and that they’re being reused.

If I just created a variable called variable one and never utilized it, it would be useless, but also it would tell us here that there was a specific error. Then, I’m going to filter to a specific type of service or name, Get-Service. I could use a name and then I can pass in the value. So that’s go with our DNS cache again.

$variable = Get-Service Dnscache
Write-Host $variable

How to run a Powershell script in Visual Code?

First off, select all the code [1], and we can right-click here and say Run Selection. Or I could say F8 [2] on the keyboard and it would execute. Sure enough, when I run it, it gives me a value, so I know it’s working correctly.

If I just cleared this bottom section and type $variable and Enter, you’ll see I can check the existence of the values.

My Write-Host option allows me to output values to the screen. Remember, that when we have a PowerShell object that’s returned, we get access to the specific properties. So you’ll see, what’s nice here is I get the IntelliSense for those options.

IntelliSense on PowerShell object
IntelliSense on PowerShell object

Of course, what’s confusing here is I don’t know which one’s which, so what’s nice about the Write-Host is we have some foreground colors that we can utilize.

So let’s select the whole thing, and make sure it works, F8. And sure enough, yellow, green, and blue. So fantastic, there’s our PowerShell script.

How do we execute the Powershell script file itself?

Right-click here on the script file that I created [1] and choose Reveal in File Explorer [2]. This will give me the path that I’m looking for. Now, what we want to be able to do is run this in the Windows Terminal. Open Windows Terminal. And you see that my path here is C:\Users\binh.phan\OneDrive – Limeade, Inc\Desktop.

Now, we can use standard syntaxes, like pushD and push directory, to get to that. Or we could also use Set-Location, and then the path. And that will set the location.

So how do we execute the My First Script?

We can just simply type my, press Tab, and because we’re in PowerShell, it automatically understands that a PS1 is an executable file and that allows me to run that within my PowerShell.

How to fix “Running PowerShell scripts is disabled”?

“Running PowerShell scripts is disabled” is a common issue encountered by users when trying to execute scripts in PowerShell. This problem often arises due to the default security settings in PowerShell, which are designed to prevent malicious scripts from running inadvertently.

To enable the execution of PowerShell scripts, you can follow these steps:

  1. Open PowerShell as Administrator: Right-click on the PowerShell icon and select “Run as Administrator” to open an elevated PowerShell session.
  2. Check Execution Policy: Use the command Get-ExecutionPolicy to check the current execution policy. If it returns “Restricted”, it means that script execution is disabled.
  3. Change Execution Policy: To enable script execution, use the command Set-ExecutionPolicy followed by the desired policy, such as Set-ExecutionPolicy RemoteSigned. This policy allows local scripts to run without a digital signature but requires downloaded scripts to be signed by a trusted publisher.
  4. Confirm Policy Change: After setting the execution policy, confirm the change by typing “Y” and pressing Enter.
  5. Run Scripts: Now that script execution is enabled, users can run PowerShell scripts by navigating to the script location and typing .\scriptname.ps1, replacing “scriptname” with the actual name of the script.

By following these steps, users can overcome the “Running PowerShell scripts is disabled” issue and execute PowerShell scripts with ease. However, exercising caution when running scripts from untrusted sources is important to avoid potential security risks.

Congratulations! You’ve taken your first steps into the world of PowerShell scripting. While this guide covers the basics, there’s still much more to learn and explore.

Leave a Comment