How-to Run a PowerShell Script – All Options Explained — LazyAdmin

PowerShell scripts are a capital way to automate tasks, gather data, or modify properties in bulk. Most PowerShell commands are entirely a unmarried tune that you can just copy and paste into PowerShell. But how do you run a complete PowerShell Script ?
There are a couple of options when it comes to running PowerShell scripts, merely from the cmd, using the PowerShell ISE, or even a schedule task .
In this article, I will explain the different options to run a PowerShell handwriting. besides, I will help you with common errors, like “ running scripts is disabled on this system ” .

Fix Running Scripts is Disabled on this System

We are going to start with a common error that prevents most users from running a PowerShell handwriting, the error “ Running scripts is disabled on this organization ”. This error is caused by the PowerShell Execution Policy.

By default option, the PowerShell Execution policy is set to Restricted. This means that PowerShell scripts won ’ thymine move at all .
Running scripts is disabled on this system
Running scripts is disabled on this system
thus to run a PowerShell handwriting, we first need to change the execution policy. For the policy we have a couple of options :

Execution Policy Description
Restricted Default option – does not allow to run any PowerShell script
Unrestricted Can run any script, shows warning for downloaded scripts
RemoteSigned Requires a digital signature for downloaded scripts. You can
run locally written scripts. You can unblock downloaded scripts
to run them without signature
ByPass You can run all scripts and no warnings are displayed
AllSigned You can only run signed scripts from trusted publishers

murder Policies are not designed as a security exemplar, but more to prevent the accidental execution of a PowerShell script. The best option is to use the RemoteSigned policy, this way you can ’ triiodothyronine run a download handwriting by accident ( you will have to unblock it first ). Read more about the execution policy in this article .

tiptoeLearn more about writing your own PowerShell scripts in this complete guide

To change the performance policy in PowerShell ( and fix “ Running scripts is disabled on this organization ) we need to start PowerShell with administrator privilege :

  1. Press Windows key + X (or right click on the start menu)
  2. Choose Windows PowerShell (admin)
  3. Run the command below:
Set-ExecutionPolicy RemoteSigned

You should now be able to run PowerShell Scripts .

Run a PowerShell Script

There are a copulate of options to run a PowerShell script. The most convenient room is to merely right-click the file and choose Run with PowerShell .
run powershell script
run powershell script

Run PowerShell Script
But this method comes with a downside. By default option, most PowerShell scripts will close the PowerShell window mechanically when the script is done. So the script gets executed, but you won ’ t be able to read any errors or output signal .
You can solve this by adding the play along line to the end of the PowerShell script, but that is not constantly a convenient choice :

Read-Host -Prompt "Press Enter to exit"

Use Windows PowerShell

personally, I prefer the depart PowerShell scripts from the command line in Windows PowerShell itself. There are two ways to do this, you can first start PowerShell and navigate to the script or open PowerShell in the decline placement from the internet explorer .
We start with the latter, opening the PowerShell in the correct location from the explorer. In the internet explorer, locate the script that you want to run, in this lawsuit, c : \temp\powershell .

  1. Right-click on the blank area in Explorer (not on the PowerShell file)
  2. Choose Open PowerShell window here
    (In Windows 11 you will need to select Open in Windows Terminal)

As you can see, PowerShell is started in the folder c : \temp\PowerShell. We can immediately run the script by plainly typing the filename ( type the first few letters and wardrobe Tab to autocomplete it )
execute powershell script
execute powershell script
You can besides beginning start Windows PowerShell :

  1. Right-click on the start menu (or press Windows key + X)
  2. Choose Windows PowerShell
  3. Navigate to the folder where the script is located
    cd c:\path\to\script
  4. Run the PowerShell script

run powershell script from command line
run powershell script from command line

Run PowerShell Script from CMD

If you have tried to run a PowerShell from cmd, then you might have noticed that it will just open the script in notepad. not very the resultant role we are looking for .
To run the PowerShell handwriting, we need to tell the command prompt to open the handwriting with PowerShell. We can use the come cmd for this :

PowerShell c:\path\to\script\PowerShellScript.ps1

run powershell script from cmd
run powershell script from cmd
note that you will need to specify the full path to the PowerShell script. The result of the handwriting is displayed in the command prompt .
If you want to keep the PowerShell session open, you can add the -noexit parameter to the command :

PowerShell -noexit c:\temp\PowerShell\PowerShellExampleScript.ps1

Using PowerShell ISE

When you download a handwriting from the internet it ’ sulfur constantly a commodity theme to review the script before you run it. An easy means to review and run a PowerShell script is to use the PowerShell ISE.

To open a PowerShell file in the editor, merely right-click it and choose Edit

When you use the editor for the foremost time you might need to expand the script acid first, to see the contented of the PowerShell script :

To run a handwriting in PowerShell ISE plainly click on the park free rein button in the toolbar. The results of the scripts are displayed in the console :

Run PowerShell Script as Administrator

Some PowerShell scripts require administrator prerogative to run correctly. They might need access to all folders on your system drive, or need to interact with early domain computers or servers .
The easiest way to run PowerShell scripts as an administrator is to start Windows PowerShell as administrator .

  1. Press Windows key + X (or right-click the start menu)
  2. Choose Windows PowerShell (admin)
  3. Click Yes on the User Account Security prompt

You can now run any PowerShell control or handwriting with Administrator prerogative. Simply voyage to the script ( or even drag and drop the script ) to run it .
You can besides run a PowerShell script as administrator from the command line. When you have PowerShell open, we can start a raw PowerShell summons and run it as Administrator. optionally we can tied specify the file that we want to run :

# Open a new PowerShell windows with Administrator Privilege
Start-Process PowerShell -Verb runAs

# Run PowerShell script with Administrator Privilege
Start-Process PowerShell -Verb runAs -ArgumentList "-file c:\temp\powershell\powershellexamplescript.ps1"

Wrapping Up

I hope you were able to run your PowerShell script with the steps above. Make indisputable that you have set the execution policy correctly before you run a PowerShell handwriting .
If you have any questions, just drop a gossip below. Make sure you besides read more about setting up your PowerShell profile .





reference :
Category : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *