Programming languages - Powershell

Back to Course

Lesson Description

Lession - #109 PowerShell-Scripting

Powershell scripting
As a scripting language, PowerShell is generally utilized for automating the administration of systems. It is likewise used to build, test, and deploy solutions, frequently in CI/CD conditions. PowerShell is based on the .NET Common Language Runtime (CLR>
. All data sources and results are .NET objects. Don't bother parsing text result to extricate data from output.

  • Cmdlets − Cmdlets perform normal framework organization undertakings, for instance dealing with the library, administrations, processes, occasion logs, and utilizing Windows Management Instrumentation (WMI>
  • Task situated − powerShell scripting language is task based and give supports to existing contents and order line apparatuses.
  • Predictable design − As cmdlets and system data stores utilize normal linguistic structure and have normal naming shows, data sharing is simple. The result starting with one cmdlet can be pipelined then onto the next cmdlet with practically no control.
  • Easy to Use − Simplified, order based route allows clients to explore the vault and different information stores like the document framework route.
  • Object based − PowerShell has strong article control capacities. Objects can be shipped off different apparatuses or databases straightforwardly.
  • Extensible point of interaction. − PowerShell is adaptable as autonomous software vendors and undertaking developers can construct custom devices and utilities utilizing PowerShell to control their software.
How To Run A PowerShell Script
There are two fundamental ways of making a PowerShell script:

1. The first, which will be recognizable assuming you've utilized Windows Command Line previously, is to compose scripts straightforwardly in notepad. For instance, open another notepadrecord, and compose
Compose Host "Hi World!"
Then save this document as FirstScript.ps1
You can call the script from PowerShell utilizing the command:

& "X:\FirstScript.ps1"

Furthermore, you'll see the result in PowerShell.

2. The second, substantially more impressive approach to making PowerShell scripts is to utilize the Windows PowerShell Integrated Scripting Environment (ISE>
. With ISE, you can run scripts and troubleshoot them in a GUI environment. ISE additionally includes language structure featuring, multiline altering, tab finish, particular execution, and an entire host of different highlights. It will even allow you to open numerous script windows simultaneously, which is valuable once you have scripts that call different scripts.

Example Script 1: Get The Date
How about we start with a basic script. In ISE or notepad, open another file. Type:

Compose Host get-date
And afterward save the file as GetDate.ps1
You can call the script from PowerShell utilizing the command:

& "C:\GetDate.ps1"

Furthermore, you'll see the result in PowerShell.

Example Script 2: Check assuming a File Exists
Assume you want to erase different files, you should initially verify whether the files even exist.
test-path, as the name infers, allows you to check whether components of the path exist. It'll return TRUE, in the event that all components exist, and FALSE assuming any are missing.
You just sort:

test-Path (And then the file path>