PowerShell Tutorial
What is PowerShell?
Features of Powershell
How to launch PowerShell
PowerShell Cmdlet
Cmdlet vs. Command:
Powershell Data types:
Special Variables
PowerShell Scripts
First PowerShell Script
What is PowerShell ISE?
PowerShell Concepts
Advantages of using PowerShell script
PowerShell Vs. Command Prompt
Applications of Powershell
How to Write and Run Scripts in the Windows PowerShell ISE
How to create and run scripts
- To create a new script file
- To open an existing script
- To close a script tab
- To display the file path
- To run a script
- To run a portion of a script
- To stop a running script
How to write and edit text in the Script Pane
- To enter text in the Script Pane
- To find text in the Script Pane
- To find and replace text in the Script Pane
- To go to a particular line of text in the Script Pane
- To copy text in the Script Pane
- To cut text in the Script Pane
- To paste text into the Script Pane
- To undo an action in the Script Pane
- To redo an action in the Script Pane
How to save a script
- To save a script
- To save and name a script
- To save a script in ASCII encoding
What is PowerShell?
Windows PowerShell is object-oriented automation engine and scripting language. It is designed mainly for the system administrators. It helps IT, professionals, to control & automate the administration of the Window OS and other applications.Features of Powershell
PowerShell Remoting : PowerShell allows scripts and cmdlets to be invoked on a remote machine.Background Jobs : It helps you to invoked script or pipeline asynchronously. You can run your jobs either on the local machine or multiple remotely operated machines.Transactions : Enable cmdlet and allows developers to performEvening: This command helps you to listen, forwarding, and acting on management and system events.Network File Transfer: Powershell offers native support for prioritized, asynchronous, throttled, transfer of files between machines using the Background Intelligent Transfer Service (BITS) technology.How to launch PowerShell
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 ClickStep 2) Power Shell Window OpensPowerShell 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 − Cmdlets are .NET Framework class objects It can't be executed separately Cmdlets can construct from as few as a dozen lines of code Parsing, output formatting, and error presentation are not handled by cmdlets Cmdlets process works on objects. So text stream and objects can't be passed as output for pipelining Cmdlets are record-based as so it processes a single object at a time 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:Get — To get somethingStart — To run somethingOut — To output somethingStop — To stop something that is runningSet — To define somethingNew — To create somethingPowerShell commands Following is a list of important PowerShell Commands:Get-Help: Help about PowerShell commands and PowerShellScriptingtopics Example: Display help information about the command Format-Table Get-Help Format-TableGet-Command: Get information about anything that can be invoked Example: To generate a list of cmdlets, functions installed in your machine Get-CommandGet-Service: Finds all cmdlets with the word 'service' in it. Example: Get all services that begin with "vm" Get-Service "vm*"Get-Member: Show what can be done with an object Example: Get members of the vm processes. Get-Service "vm*" | Get-MemberOther Commands: Get Module Shows packages of commands Get Content This cmdlet can take a file and process its contents and do something with it Get- get Finds all cmdlets starting with the word 'get- Example: Create a Folder New-Item -Path 'X:\Guru99' -ItemType Directory OutputPowershell 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 |
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 are commands which is written in the PowerShell language. It can be developed without using other IDE like Visual Studio and devs. | |
Scripts are text files on disk with a .ps1 extension | |
Applications are existing windows programs. | |
Tells the cmdlet not to execute, but to tell you what would happen if the cmdlet were to run. | |
Instruct the cmdlet to prompt before executing the command. | |
Gives a higher level of detail. | |
Instructs the cmdlet to provide debugging information. | |
Instructs the cmdlet to perform a specific action when an error occurs. Allowed actions continue, stop, silently- continue and inquire. | |
It specifies the variable which holds error information. | |
Tells the cmdlet to use a specific variable to hold the output information | |
Instructs the cmdlet to hold the specific number of objects before calling the next cmdlet in the pipeline. |
|
|
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. |
.ps1
), script data files (.psd1
), and script module files
(.psm1
).
These file types are syntax colored in the Script Pane editor.
Other common file types you may open in the Script Pane are configuration files (.ps1xml
), XML files, and text files.
Note
The Windows PowerShell execution policy determines whether you can run scripts and load Windows
PowerShell profiles and configuration files.
The default execution policy, Restricted, prevents all scripts from running, and prevents loading profiles.
To change the execution policy to allow profiles to load and be used, see
Set-ExecutionPolicy and
about_Signing.
.ps1
) is created, but it can be saved with a new name and extension.
Multiple script files can be created in the same PowerShell tab.
*.ps1
)'.
Click .ps1
), script data files (.psd1
), and script module files (.psm1
) as Unicode (BigEndianUnicode) by default.
To save a script in another encoding, such as ASCII (ANSI), use the $psISE.CurrentFile.SaveAs("MyScript.ps1", [System.Text.Encoding]::ASCII)
The following command replaces the current script file with a file with the same name, but with ASCII encoding.
$psISE.CurrentFile.Save([System.Text.Encoding]::ASCII)
The following command gets the encoding of the current file.
$psISE.CurrentFile.encoding
Windows PowerShell ISE supports the following encoding options: ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, UTF8, and Default.
The value of the Default option varies with the system.
Windows PowerShell ISE doesn't change the encoding of script files when you use the Save or Save As commands.