How to Automate Windows with PowerShell

0
102
How to Automate Windows with PowerShell

Here we can see, “How to Automate Windows with PowerShell”

What is PowerShell Language?

PowerShell language may be a high-level proprietary programming syntax developed by Microsoft for the key purpose of enabling system administrators to automate actions and configurations. The language is predicated on object-oriented standards but can only be utilized in Windows environments. It’s a part of the .NET framework and typically has C# code underlying its functions, although knowledge of C# isn’t a prerequisite for learning PowerShell. The closest comparison to the PowerShell language is Perl, which is employed in similar scenarios on Linux environments.

With the PowerShell language, each unique function is mentioned as a cmdlet. A cmdlet has one or more sets of defined actions and is capable of returning a .NET object. A number of the foremost basic cmdlets that come pre-configured with PowerShell are ones for navigating through a folder structure and moving or copying files.

Introduction

PowerShell is the most powerful automation tool that Microsoft has got to offer, and its both a shell and a scripting language.

Please note that this series is predicated on PowerShell 3, which ships with Windows 8 and Server 2012. If you’re running Windows 7, please download the PowerShell 3 update before you continue.

Also See:  Android Fix: App could not be downloaded due to an Error 497

Meet the Console and the ISE

There are two ways of interacting with PowerShell out of the box, the Console and therefore the Integrated Scripting Environment – also referred to as the ISE. The ISE has vastly improved from the hideous version that shipped with PowerShell 2 and may be opened by pressing the Win + R keyboard combination to mention a run box, then typing powershell_ise and pressing enter.

As you’ll see, the ISE sports a split view so that you’ll rapidly script while still having the ability to ascertain the end in the lower half of the ISE. Rock bottom half the ISE, where your script results are printed, can also be used as a REPL prompt – very similar to prompt. The v3 ISE finally added support for IntelliSense in both the script pane and the interactive Console.

Alternatively, you’ll interact with PowerShell using the PowerShell Console, which I will be able to be used for many of this series. The PowerShell Console behaves very similarly to the prompt – you merely enter commands, and it spits out the results. To open the Windows PowerShell Console, press the Win + R keyboard combination to open a run box, sort PowerShell, and then press enter.

REPL prompts like this are awesome for fast gratification: you enter a command and get results. While the Console doesn’t offer IntelliSense, it does offer something called tab completion, which functions much an equivalent – start typing a command and press tab to cycle through possible matches.

Using the Help System

In past versions of PowerShell, help files were included once you installed Windows. This was an honest solution for the foremost part but left us with a big problem. When the PowerShell help team prevented performing on the assistance files, the PowerShell developers were still busy coding and making changes. This meant that when PowerShell shipped, the assistance files were incorrect because they didn’t contain the newer changes made to the code. to unravel this problem, PowerShell 3 has no help files out the box and includes an updatable help system. This suggests before you do anything, you’ll want to download the newest help files. You’ll do this by opening a PowerShell Console and running:

Update-Help

Congratulations on running your first PowerShell command! the reality is that the Update-Help command features a lot more options than simply running it, and to ascertain them, we’ll want to look at the assistance for the command. to look at the assistance for a command, you merely pass the name of the command you would like help with to the Name parameter of the Get-Help command, for example:

Get-Help –Name Update-Help

You are probably wondering the way to interpret all that text anyway, and I mean, why are there too much information under the syntax section, and why are there numerous brackets everywhere the place? First things first: the rationale is that there are two blocks of data under the syntax section because they represent alternative ways to run the command. These are technically called parameter sets, and you’ll only use one at a time (you can’t mix parameters from different sets). So, for example, within the above screenshot, you’ll see that the highest parameter set features a SourcePath parameter while rock bottom doesn’t. the rationale being that you would use the highest parameter set (the one that has SourcePath) if you were updating your help files from another machine on your network that had already downloaded them. At the same time, you wouldn’t get to specify a source path if you only wanted to grab the newest files from Microsoft.

To answer the second question, there’s a particular syntax that helps files follow and here it is:

  • Square brackets around a parameter name and its type mean it’s an optional parameter, and therefore the command will work just fine without it.
  • Square brackets around the parameters name mean the parameters is a positional parameter.
  • The thing to the proper of a parameter within the angled brackets tells you the info type the parameter is expecting.
Also See:  NVIDIA RTX 3080 Ti Cards Start Shipping on May 31st, Arrives June 9th

While you ought to learn to read the assistance file syntax, if you’re ever unsure of a few particular parameters, append –Full to the top of your get help command and scroll right down to the parameters section, where it’ll tell you a touch more about each parameter.

Get-Help –Name Update-Help –Full

The last item you would like to understand about the assistance system is how to use it to get commands, which is very easy. You see, the PowerShell accepts wildcards almost anywhere, so using them alongside the Get-Help command allows you to discover commands. For instance, I’m trying to find commands that affect Windows Services:

Get-Help –Name *service*

Sure, all of this information won’t be handy of the bat, but trust me, take the time and find out how to use the assistance system. It comes in handy all the time, even to advanced scripters who are doing this for years.

Security

This wouldn’t be a correct introduction without mentioning security. the most important worry for the PowerShell team is that PowerShell becomes the newest and greatest attack point for script kiddies. They need to put a couple of security measures in situ to make sure that this doesn’t happen, so let’s glance at them.

The most basic sort of protection comes from the fact that the PS1 file extension (the extension wont to denote a PowerShell script) isn’t registered with a PowerShell host, its registered with Notepad. Meaning if you double click on a file, it’ll open with Notepad rather than running.

Secondly, you can’t run scripts from the shell by just typing the script’s name; you’ve got to specify the complete path to the script. So if you wanted to run a script on your C drive, you’d need to type:

C:\runme.ps1

Or, if you’re already at the basis of the C drive, you’ll use the following:

.\runme.ps1

Finally, PowerShell has something called Execution Policies, which stop you from just running the unspecified script. In fact, by default, you can’t run any scripts and wish to vary your execution policy if you would like to be allowed to run them. There are 4 notable Execution Policies:

  • Restricted: This is often the default configuration in PowerShell. This setting means no script can run, no matter its signature. the sole thing which will be run in PowerShell with this setting is a private command.
  • AllSigned: This setting does allow scripts to run in PowerShell. The script must have an associated digital signature from a trusted publisher. There’ll be a prompt before you run the scripts from trusted publishers.
  • RemoteSigned: This setting allows scripts to be run but requires that the script and configuration files downloaded from the web have an associated digital signature from a trusted publisher. Scripts run from the local computer don’t get to be signed. In addition, there are not any prompts before running the script.
  • Unrestricted enable unsigned scripts to run, including all scripts and configuration files downloaded from the web. This may include files from Outlook and Messenger. the danger here is running scripts with no signature or security. So we recommenced that you simply never use this setting.

To see what your current Execution Policy is about to, open a PowerShell Console and type:

Get-ExecutionPolicy

For this course and most other circumstances, the RemoteSigned Policy is the best, so plow ahead and alter your policy using the subsequent.

Note: this may be got to be done from an elevated PowerShell Console.

Set-ExecutionPolicy RemoteSigned

That’s all for this point folks, see you tomorrow for a few more PowerShell fun.

Disclaimer: the right term for a PowerShell command may be a cmdlet, and from now on, we’ll use this correct terminology. It just felt more appropriate to call them commands for this introduction.

Conclusion 

I hope you found this guide useful. If you’ve got any questions or comments, don’t hesitate to use the shape below. 

User Questions:

  1. Should I delete Windows PowerShell?

Yes, you’ll uninstall Windows PowerShell if you do not use it and also can download and install it later if you feel you would like it. Microsoft Windows PowerShell may be a new command-line shell and scripting language designed for system administration and automation.

  1. Is Windows PowerShell safe?

The good news is that PowerShell is safer by default than previous scripting environments, thanks to the execution policy and signing requirements of PowerShell scripts. There’ll certainly be vulnerabilities exposed and brought the advantage of by the various ne’er-do-wells of the planet in PowerShell’s lifetime.

  1. What is malicious PowerShell?

A malicious script looks to the human eye as another common Powershell script that features a hidden intention. That intention could be extracting, deleting, or renaming files, installing extraneous programs, downloading or sending files through the web without the end-user’s consent.

Also See:  powershell commands windows 10
  1. Windows PowerShell Scripting Tutorial for Beginners

Windows PowerShell Scripting Tutorial for Beginners from Netwrix

  1. I want to use Powershell to automate things, but I’m unsure what

I want to use Powershell to automate things but I’m not sure what from sysadmin