Tuesday, November 13, 2018

Different Ways to Use PowerShell

Table of Contents

Introduction

Using PowerShell on the command line is not the only way to use PowerShell.

You can write PowerShell scripts. You can run PowerShell from the old Command Prompt. You can run PowerShell from batch scripts, other PowerShell scripts, scheduled tasks, etc.

Back to the TOC

Running PowerShell from the old Command Prompt

First, lets look at the difference between running PowerShell from the PowerShell command line interface (powershell.exe) vs running PowerShell from the old Command Prompt (cmd.exe).

To run PowerShell commands from within powershell.exe, you just use the language as usual:

Get-Process
To run the exact same command from cmd.exe, do this:
powershell.exe -Command "Get-Process"
By the way, if you're running a Command Prompt and you want to change to PowerShell, just type powershell and push enter:
powershell
or
powershell.exe
...and if you're running a PowerShell window and you want to use the old Command Prompt, just type cmd and push enter:
cmd
or
cmd.exe
What happens when you do this switching between cmd.exe and powershell.exe, is that you open another "layer" of operation on your command line. To go back to the previous "layer", type exit:
exit
...then you'll go back to whatever you started with.

Back to the TOC

Running PowerShell from a PowerShell Script

So, if you have a bunch of commands that you run over and over, just save each command in a text file on its own line and make sure that file has the .ps1 extension when you save it.

After you've saved your script, like script.ps1 for example, there are multiple ways to run it.

P.S. If your script won't run, you likely don't have a compatible ExecutionPolicy set. Use Get-ExecutionPolicy to see what your current policy is set to. To change your ExecutionPolicy, run PowerShell as Administrator and use the Set-ExecutionPolicy cmdlet to change to a compatible policy. Here's a list of the policies described in human words:

Execution Policy Description
AllSigned Requires any .ps1 script to be digitally signed from a trusted publisher.
Bypass Any .ps1 script should work.
Default Sets the ExecutionPolicy to Restricted.
RemoteSigned Requires downloaded .ps1 scripts to be digitally signed by a trusted publisher.
Restricted This basically blocks all .ps1 scripts.
Undefined Removes the current execution policy unless it was set by Group Policy.
Unrestricted All .ps1 scripts should run but download .ps1 scripts prompt for permission.

I got this information from simply using the Get-Help cmdlet on Set-ExecutionPolicy like this:

Get-Help Set-ExecutionPolicy -Parameter ExecutionPolicy

If you want to unblock a downloaded file because you're sick of being prompted about it, you can use the Unblock-File cmdlet.

Back to the TOC

Running a PowerShell Script by Clicking the Icon

If you want to run a PowerShell script by clicking an icon, you can. But only if you right-click the icon and choose "Run with PowerShell."

Here's something to notice regarding almost any script in almost any scripting language: The purpose of a script is to execute all the commands as quickly as possible and close the command line window. If your script runs quick and closes before you can read anything, that's exactly what it's supposed to do.

If you want your script to stay open after execution, use the PAUSE keyword in your .ps1 script.

Actually, there is one other way to run a PowerShell script by clicking - but you have to write a batch script to launch your PowerShell script. Let's say you already have a script written C:\script.ps1. Now just write a simple batch script to launch it (so you can double click the batch script):

:: PowerShell script launcher

powershell.exe ^
    -ExecutionPolicy Bypass ^
    -File "C:\script.ps1"
or, if your script has parameters:
:: PowerShell script launcher

powershell.exe ^
    -ExecutionPolicy Bypass ^
    -Command "C:\script.ps1 -MyParameter myargument"

Back to the TOC

Running a PowerShell Script via a PowerShell Window

If you are using PowerShell on the command line, you can run a script by typing in the relative or absolute file path to your script file:

C:\folder\script.ps1
or
.\script.ps1

Back to the TOC

Running a PowerShell Script via Command Prompt

If you're running your script from the old Command Prompt, make sure to tell the Command Prompt to execute the program called "powershell.exe" and give that program an argument which is the path to your script.

powershell.exe C:\folder\script.ps1
or
powershell.exe .\script.ps1

Back to the TOC

Running a PowerShell Script from Another PowerShell Script

You can write a PowerShell script that executes another PowerShell script like this - first write one script, script1.ps1:

# script1.ps1
Write-Host "This sentences is from script1.ps1."
PAUSE
...next write another script, script2.ps1, that calls your first script, script1.ps1:
# script2.ps1
.\script1.ps1
Write-Host "This sentence is from script2.ps1."
PAUSE

Back to the TOC

Running a PowerShell Script or Command from a Batch Script

Remember when using the old Command Prompt, you have to use powershell.exe to run PowerShell? Batch scripts use the old Command Prompt technology to run so the same rule applies. Here's a few examples of some batch scripts used to run PowerShell code.

The cool thing about writing a batch script to execute your PowerShell script is that you can just double-click the batch script instead of having to right-click and select a context menu option to run the code. ALSO, if you need to run the batch script as Admin, you can do that just by selecting the "run as Administrator" context menu option unlike with PowerShell scripts. To run a PowerShell script as Admin, you'd have to open a new PowerShell window as admin then execute your script afterwards. Making simple batch scripts to launch your PowerShell scripts just makes things easy.

:: Batch Script 1

@ECHO OFF

SET _title=Batch Script 1
TITLE %_title%
ECHO %_title% loading...

POWERSHELL.EXE "Get-Process | Select-Object -First 5"

PAUSE
:: Batch Script 2

@ECHO OFF

SET _title=Batch Script 2
TITLE %_title%
ECHO %_title% loading...

POWERSHELL.EXE ^
    -ExecutionPolicy Bypass ^
    -File ".\script1.ps1"

PAUSE

Notice that I pass the argument Bypass to the parameter -ExecutionPolicy. This way, I don't have to know what the ExecutionPolicy is on the computer that will be running this .ps1 script.

Back to the TOC

Using PowerShell with Task Scheduler

If you want the computer to execute your PowerShell script as a scheduled task, you have a few options.

Back to the TOC

Using a Scheduled Task with powershell.exe to Execute PowerShell Code

First, I recommend making your own folder in Task Scheduler by left-clicking "Task Scheduler Library" then right-clicking it and choosing "New Folder..."

Then, open your new folder and to the far right under "Actions" select "Create Basic Task..." Next give it a name and description. Next for testing, tell it to execute daily. Next give it a time, any time and tell it to repeat every "1" day. Next choose "Start a program." Next listen closely...

Now you'll see three boxes. The first is for the program to run, which in our case will be powershell.exe. So in this first text box, you need to type this:

powershell.exe
or, if that doesn't work:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
The second box is where we put in the parameters as if we were trying to execute the script from the old Command Prompt. So type in something like this:
-ExecutionPolicy Bypass -File "C:\script.ps1"
or, if your script has parameters:
-ExecutionPolicy Bypass -Command "C:\script.ps1 -MyParameter myargument"
Leave the the third text box blank - the one that says "Start in (optional)."

Now to test your script, you don't have to wait for your time trigger to elapse. Just right click your task and select "Run."

If you have trouble with your task, check the history tab. If history is disabled, you can toggle that setting on the right under "Actions."

Back to the TOC

Using a Scheduled Task with a Batch Script to Execute PowerShell Code

To make the scheduled task simpler, set it up just like we did up to the point with the three boxes for the program to run, its arguments, and a starting path. But this time we're going to simply create a batch script and put that in the text box for the program to run. Here's an example batch script:

:: PowerShell Script Launcher
powershell.exe ^
    -ExecutionPolicy Bypass ^
    -File "C:\script.ps1"
or if your script has parameters:
:: PowerShell Script Launcher
powershell.exe ^
    -ExecutionPolicy Bypass ^
    -Command "C:\script.ps1 -MyParameter myargument"

Back to the TOC

Conclusion

Now you know all kinds of ways to use PowerShell. I did leave out one last way to use PowerShell - by using modules. It's a little more advanced so I'll write that in another post.

Back to the TOC