Monday, October 17, 2022

Notes About Powershell: Very Basic Powershell ISE Usage

Start by opening the Powershell ISE. It should be available in the "Type here to search" window of Windows 10, or similar. Note that the ISE (Integrated Scripting Environment) is essentially a simple Integrated Development Environment (IDE). Initially, it'll likely open only to a blue screen where you are in the Powershell "shell", where you can type commands, etc. For example, type:

dir

to get a listing of the current directory. This is a backwards-compatible (to the days of MS-DOS, even) command. The Powershell equivalent of doing the same thing is:

Get-Childitem

Yes, that does seem like over-kill, but Microsoft is going for consistency within the Powershell environment, with every command having an approved verb, Get in this case, followed by the "gist" of what the command is about, with the Childitem referring to the directory items.

You can put these Powershell-type commands in a script, to run like a program. To do this, go to File/New to open a new script window.

In here, you can put your dir command. Or better, use the Powershell equivalent: Get-Childitem. You might see the ISE trying to help you find the correct command/item with it's type-ahead prompts. (And you might not; it seems to be pretty hit-or-miss.)

Now, run your script by clicking on the green arrow in the menubar area. The results should show in the blue shell area below the text editing area.

Now if you save the script to a file, it'll automatically save with a ".ps1" extension, and you can open it later for editing. But once you save it, Powershell considers it a "real" script, and will refuse to run it because running scripts is against the default Powershell policy. Give it a try to see what I mean (assuming it hasn't already been fixed on your machine).

To fix that, fire up a Powershell session as Administrator, and run:

PS > Set-ExecutionPolicy

and select the "All" option. You can then exit out of the Admin session of Powershell, and restart your normal-user session of the Powershell ISE, and you should now be able to run your script.

No comments: