Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Master PowerShell: Beginner's Guide to Powerful Scripting


Let’s start learning Powershell Lessons. First, we will learn:

What is PowerShell?

Windows PowerShell is an object-oriented automation engine and scripting language. It is designed mainly for IT professionals and system administrators to control & automate the administration of Windows OS and other applications. It provides compelling new concepts to extend the knowledge you have gained and scripts you have created within the Windows Command Prompt and Windows Script Host environments.

It combines the flexibility of scripting, command-line speed, and the power of a GUI-based admin tool. It allows you to solve problems efficiently by helping system admin to eliminate future manual labor hours. We will go through all the important aspects which you should know to learn PowerShell.

In this Powershell tutorial for beginners, you will learn Powershell basics like:

This is a complete guide to Powershell scripting basics… let’s begin!

Why Use Powershell?

Here, are some important reason for using Powershell:

PowerShell History

PowerShell first version 1.0 was released in 2006. Today, PowerShell is at version 7.2. As the year and version gone by, PowerShell’s capabilities and hosting environments grew significantly.

Let See Version wise History of Powershell:


Next in this Powershell scripting tutorial, we will learn about features of Powershell.

Features of Powershell

How to launch PowerShell

Now in this Powershell script tutorial, we will learn how to launch Powershell on Windows OS.

PowerShell is pre-installed in all latest versions of Windows. We need to launch PowerShell for that we need to follow the given steps:

Step 1) Search for PowerShell in Windows. Select and Click

Master PowerShell: Beginner s Guide to Powerful Scripting


Step 2) Power Shell Window Opens

Master PowerShell: Beginner s Guide to Powerful Scripting

PowerShell Cmdlet

A cmdlet which is also called Command let is a is a lightweight command used in the Window base PowerShell environment. PowerShell invokes these cmdlets in the command prompt. You can create and invoke cmdlets command using PowerShell APIS.

Cmdlet vs. Command:

Cmdlets are different from commands in other command-shell environments in the following manners ?

Most of the PowerShell functionality comes from Cmdlet’s which is always in verb-noun format and not plural. Moreover, Cmdlet’s return objects not text. A cmdlet is a series of commands, which is more than one line, stored in a text file with a .ps1 extension.

A cmdlet always consists of a verb and a noun, separated with a hyphen. Some of the verbs use for you to learn PowerShell is:

PowerShell commands

Following is a list of important PowerShell Commands:

Get-Help: Help about PowerShell commands and topics

Example: Display help information about the command Format-Table

Get-Help Format-Table

Master PowerShell: Beginner s Guide to Powerful Scripting

Get-Command: Get information about anything that can be invoked

Powershell Script Example: To generate a list of cmdlets, functions installed in your machine

Get-Command

Master PowerShell: Beginner s Guide to Powerful Scripting

Get-Service: Finds all cmdlets with the word ‘service’ in it.

Example: Get all services that begin with “vm”

Get-Service "vm*"

Master PowerShell: Beginner s Guide to Powerful Scripting

Get- Member: Show what can be done with an object

Example: Get members of the vm processes.

Get-Service "vm*" | Get-Member

Master PowerShell: Beginner s Guide to Powerful Scripting

Other Commands:

Example: Create a Folder

New-Item -Path 'X:\Guru99' -ItemType Directory

Output

Master PowerShell: Beginner s Guide to Powerful Scripting

Powershell Data types:

Master PowerShell: Beginner s Guide to Powerful Scripting

Powershell Data Types

Special Variables

Special Variable Description $Error An array of error objects which display the most recent errors $Host Display the name of the current hosting application $Profile Stores entire path of a user profile for the default shell $PID Stores the process identifier $PSUICulture It holds the name of the current UI culture. $NULL Contains empty or NULL value. $False Contains FALSE value $True Contains TRUE value

PowerShell Scripts

Powershell scripts are store in .ps1 file. By default, you can’t run a script by just double-clicking a file. This protects your system from accidental harm. To execute a script:

Step 1: right-click it and click “Run with PowerShell.”

Master PowerShell: Beginner s Guide to Powerful Scripting

Moreover, there is a policy which restricts script execution. You can see this policy by running the Get-ExecutionPolicy command.

You will get one of the following output:

Steps to Change Execution Policy

Step 1) Open an elevated PowerShell prompt. Right Click on PowerShell and “Run as Administrator”

Master PowerShell: Beginner s Guide to Powerful Scripting

Step 2) Enter the Following commands

  1. Get-ExecutionPolicy
  2. Set-executionpolicy unrestricted
  3. Enter Y in the prompt
  4. Get-ExecutionPolicy

Master PowerShell: Beginner s Guide to Powerful Scripting

First PowerShell Script

In a notepad write the following command

Write-Host "Hello, Guru99!"

PowerShell Scripts have an extension ps1. Save the file as FirstScript.ps1

Master PowerShell: Beginner s Guide to Powerful Scripting

In Powershell, call the script using the command

& "X:\FirstScript.ps1"

Master PowerShell: Beginner s Guide to Powerful Scripting

What is PowerShell ISE?

The Windows PowerShell Integrated Scripting Environment(ISE) is the default editor for Windows PowerShell. In this ISE, you can run commands, writer test, and debug scripts in an in a window base GUI environment. You can do multiline editing, syntax coloring, tab completion, selective execution and lots of other things.

Windows PowerShell ISE also allows you to run commands in a console pane. However, it also supports panes that you can use to simultaneously view the source code of your script and other tools which you can plug into the ISE.

You can even open up multiple script windows at the same time. This is specifically useful when you are debugging a script which uses functions defined in other scripts or modules.

Master PowerShell: Beginner s Guide to Powerful Scripting

PowerShell ISE

The same script we created in notepad, can be created in ISE

  1. Paste code into the editor
  2. Save Script
  3. Use F5 to run the script
  4. Observe output in the console

Master PowerShell: Beginner s Guide to Powerful Scripting

Sample 2:

The following code will give the Free Virtual Memory in your machine

Get-WmiObject -Class Win32_OperatingSystem –ComputerName localhost |
Select-Object -Property CSName,FreeVirtualMemory 

Master PowerShell: Beginner s Guide to Powerful Scripting

PowerShell Concepts

Now in this Powershell for beginners tutorial, we will learn about important Powershell concepts:

Cmdlets Cmdlet are build-command written in .net languages like VB or C#. It allows developers to extend the set of cmdlets by loading and write PowerShell snap-ins. Functions Functions are commands which is written in the PowerShell language. It can be developed without using other IDE like Visual Studio and devs. Scripts Scripts are text files on disk with a .ps1 extension Applications Applications are existing windows programs. What if Tells the cmdlet not to execute, but to tell you what would happen if the cmdlet were to run. Confirm Instruct the cmdlet to prompt before executing the command. Verbose Gives a higher level of detail. Debug Instructs the cmdlet to provide debugging information. ErrorAction Instructs the cmdlet to perform a specific action when an error occurs. Allowed actions continue, stop, silently- continue and inquire. ErrorVariable It specifies the variable which holds error information. OutVariable Tells the cmdlet to use a specific variable to hold the output information OutBuffer Instructs the cmdlet to hold the specific number of objects before calling the next cmdlet in the pipeline.

Advantages of using PowerShell script

PowerShell Vs. Command Prompt

PowerShell Command Prompt PowerShell deeply integrates with the Windows OS. It offers an interactive command line interface and scripting language. Command Prompt is a default command line interface which provided by Microsoft. It is a simple win32 application that can interact and talk with any win32 objects in the Windows operating system. PowerShell uses what are known as cmdlets. It can be invoked either in the runtime environment or the automation scripts. No such features offer by command prompt. PowerShell considers them as objects. So the output can be passed as an input to other cmdlets through the pipeline. Command Prompt or even the *nix shell, the output generated from a cmdlet is not just a stream of text but a collection of objects. The PowerShell is very advanced regarding features, capabilities and inner functioning. Command prompt is very basic.

Applications of Powershell

Today, PowerShell has become an ideal choice for IT administrators as it eases management operation and effort in large corporate networks. For example, let’s assume that you are managing a large network which contains more than four hundred servers. Now you want to implement a new security solution. This security solution depends on a certain service which needs to run on those servers.

You can surely log in to each server and see if they have that service install and running or not. However, it certainly takes a lot of human errors as your staff needs to spend lots of time on this non-productive process.

However, if you use PowerShell, then you could complete this task in just a few minutes. That’s because the entire operation is done with a single script which gathers information about the services running on the servers.

Summary


C Language

  1. Build a Raspberry Pi Obstacle‑Avoiding Robot – A Beginner’s Guide
  2. CNC Routers for Beginners: Master the Market with Effortless Precision
  3. C# Windows Forms Development: A Hands‑On Tutorial
  4. Master C Programming: Comprehensive PDF Tutorial for Beginners
  5. Mastering Groovy: A Beginner’s Guide to Scripting on the Java Platform
  6. Master Django: Beginner's Guide to Features, Architecture & History
  7. Beginner’s Guide to Using a 3D Pen: Easy Steps & Tips
  8. Essential Servo Motor Guide for Beginners: Learn Basics & Applications
  9. Beginner's Guide to Coding: Master Programming Basics in 2021
  10. Start Programming Industrial Robots: A Beginner's Guide