An introduction to Windows PowerShell for beginners

PowerShell corresponds to one of the best tools of Windows operating system. It has been available since December 2006 and is enabled for Windows XP and its later versions .

However, despite being such an interactive program, it is a bit complex to use, which is why we will discuss in this article how you can run and write the scripts in this wizard.

What is PowerShell?

PowerShell is a connected interface that executes commands or instructions directly to the computer , both for the server it uses and for certain applications installed on the system.

Its main requirement is the prior installation of a resource called .NET with a version no less than 2.0 and in addition to being admissible mostly in Windows, it can also be downloaded alternatively in more complex operating systems such as Linux and MacOS.

This program differs from CMD (Command Console) in that its functions are much more extensive, allowing deeper changes to be made to the system server and to other more specific programs.

In addition, it is more modern than the CMD and corresponds to a much more versatile tool than the latter, thanks to the type of programming language that is used in executing the commands.

How to run powershell script from command line

To execute scripts you must bear in mind that PowerShell must have full permissions on the server , this being a fundamental requirement for its use.

Run or write simple commands in PowerShell

The procedure to execute simple scripts in PowerShell is as follows:

  • Go to the “Start” section of Windows.
  • Once you are there, search for “Windows PowerShell” .
  • When the corresponding result appears, right-click on it.
  • Then, you must click on the option “Run as administrator” .

Then confirm the action in the pop-up menu that will appear.

  • When you have done this, the open PowerShell program will be displayed on your PC and you can run the commands you want.

Note: The tool allows you to place scripts in writing or paste them into the system in which case you just have to copy the command and proceed to right-click on the menu.

Windows PowerShell ISE is an assistant to the PowerShell program as such, which allows creating, saving, modifying and also writing new commands in said tool.

However, for the latter, these guidelines must be followed:

  • Enter “Windows PowerShell ISE” from the quick options of the “PowerShell” program.
  • Go to the “View” section that you notice at the top of the menu.
  • Select “Go to Script Panel . “
  • Finally write the selected command.

Likewise, this tool will allow you to cut, copy or paste commands directly, although for this you must use the buttons found in the upper section of the writing menu.

In addition to this, you can overwrite a script already placed in the writing area, but to do so you have to click “Ctrl + H” and search specifically for the command line you want to modify.

Run script from Windows PowerShell ISE

The procedure to execute a script is very simple. Enter Windows PowerShell ISE and click on “Run Script” , a button found on the top menu.

Likewise, you can go to “File” and from there click on “Execute” so that the command begins to carry out the pre-established processes.

However, this program also allows you to run only part of the Script, but to do this you must go to the third section of the quick options menu and click on “Run selection” and then specify which part of the script you want.

Run new scripts in PowerShell

Despite the fact that PowerShell is an executor program, it allows creating scripts your own custom scripts, although to do this, the following steps must be taken into account:

  • Firstly, access “PowerShell ISE” from the search engine on your PC.
  • Then, when the program is displayed on your screen, click on the “New” alternative that is shown in the quick options at the top.
  • Finally write at the bottom the new script to perform.

However, setting a new script only works by following the rules below:

  1. A code name must be established with the sign “#” , for purposes of modification and identification.
  2. You must put the type of variable you want to designate: For this case, the symbol of “$” is used , then the sign of “=” and then an alternate value is established.
  3. Variables must have an identification method: This is in order to set the created type and specify the digits used in the script.

powershell script example Credits: Bryan Cafferky

After this you have to save it by executing this procedure:

  • Click on the “File” item in the top section.
  • Click on the “Save As” menu .

  • Put the name you want without any pre-established signs in the “File name” box .
  • Proceed to indicate “PowerShell Scripts (* .ps1)” in the “Save as type” alternative .
  • Finally press “Save” at the bottom.

Run already created scripts

If you have already created a Script and stored it in your directory, you can open it directly in Windows PowerShell ISE as follows:

  • As a first step, you must access “Windows PowerShell ISE” .
  • Then, you have to click on “File” in the upper left.
  • Click on “Open …” .
  • Before this you will be taken to the designated documents of the PC, where you can select the script to use.

Example of PowerShell scripts

We start a PowerShell window and execute:

Get-Command

In this way we can see the commands (or cmdlets), functions, aliases that we have available to execute.

The commands use a naming system, whereby each command is made up of two parts Verb and Name, separated by a hyphen. For example: Get-Command, Get-Service, Stop-Service, Start-Service …

Each command has different parameters that we can use to define the specific command to execute. For example: If we want to obtain only the aliases we execute

Get-Command -CommandType Alias

With all the commands we can use parameters. There are some parameters that are common to all commands, such as -WhatIf, -Verbose … and others that are specific to each command, such as the one we have seen -CommandType

Another basic example but adding one of the most important aspects of PowerShell, the pipeline between commands, that the output of one command serve as input for another.

If we execute:

Get-Service | Where-Object { $_.Status -eq “Running”} | Sort DisplayName | Select-Object ServiceName,DisplayName | Out-Grid View

We get a window with the list of services that are in Running status, ordered alphabetically by description. Later we will see in detail what has happened in this command.

Use of aliases

If we execute:

dir C:\

We see that we get a list of the files.

But what if instead of “dir” we use “ls”

ls C:\

We get the same result. So do Linux commands work in PowerShell? No. What we have here is Alias. Commands that are actually links to other commands and Microsoft has had the idea to create the alias “ls” by default.

We see that both “dir” and “ls” are Aliases of the same command Get-ChildItem

What happens if we run :

Get-ChildItem C:\

Well, we have the same output as with “dir” and with “ls”.

Now we are going to see how a simple command such as the «dir» command, using the corresponding parameters, expand its potential. For example:

dir C:\*.txt -File -Recurse

With this command we obtain all the files with the existing txt extension in the C: \ drive.

Another example:

dir “C:\Program Files” -File -Recurse | Sort-Object Count -Descending | Select-Object Name, Count | Out-GridView

This will display a list with the number of files of each type according to their extension, arranged in decreasing order from largest to smallest number of files


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Marianne elanotta

As a graduate in communication technologies, Marianne likes to share the latest technological advances in various fields. She likes to program in Java OO and Javascript and prefers to work on Open source operating systems. She likes to play chess and computer games in her spare time along with her 2 kids.

Leave a Reply