Installation and setup of PowerShell.
PowerShell comes in two main variants: Windows PowerShell and PowerShell Core (which is cross-platform). Here are the steps to install and set up both versions.
Installing Windows PowerShell
Windows PowerShell is pre-installed on Windows 7 and later versions. To check the version of PowerShell installed:
Open PowerShell:
Press Win + R, type powershell, and press Enter.
Check PowerShell Version:
Run the following command:
powershell
# Check Powershell Version
$PSVersionTable.PSVersion
If you need a more recent version, update your Windows to the latest service pack or feature update.
# Install powershell or update
winget install –id Microsoft.Powershell –source winget
How to Configuring PowerShell
Setting Execution Policy
By default, PowerShell’s execution policy restricts running scripts to prevent malicious activities. You can change the execution policy to allow scripts to run:
PowerShell as Administrator:
Right-click the PowerShell icon and select “Run as Administrator“.
# Run the following command to set the execution policy. Common options are Restricted,RemoteSigned, and Unrestricted
Set Execution Policy# Setup to run RemoteSigned
Set-ExecutionPolicy RemoteSigned
#Confirm the change when prompted.
How to install module
You can install modules from the PowerShell Gallery
Install a Module:
#Run the following command to install a module (e.g., Azure module)
Install-Module -Name Az -AllowClobber -Scope CurrentUser
How to import a Module:
#After installing, you need to import the module to use its cmdlets
Import-Module -Name Az
How to do using PowerShell ISE
PowerShell Integrated Scripting Environment (ISE) is a GUI-based application for writing, testing, and debugging PowerShell scripts.
#Open PowerShell ISE (Press Win + R, type powershell_ise, and press Enter.)
Using ISE: You can write scripts in the scripting pane and execute them using the console pane.
Basics of the PowerShell console.
The PowerShell console is a command-line interface that allows users to interact with the operating system by running commands and scripts. Here are the fundamental aspects and commands to get started with the PowerShell console:
Opening the PowerShell Console
Windows:
Press Win + R, type powershell, and press Enter.
Open the terminal and type powershell, then press Enter.
The prompt in PowerShell typically looks like this:PS C:\Users\YourUsername>
PS: Indicates PowerShell.
C:\Users\YourUsername: Shows the current directory path.
Basic Commands
Get-Command:
Lists all available cmdlets, functions, workflows, aliases, and applications.
Clears the console screen.
#type simply cls or
Clear-Host
Using PowerShell ISE (Integrated Scripting Environment)
PowerShell Integrated Scripting Environment (ISE) is a graphical user interface (GUI) that provides a rich environment for developing, testing, and running PowerShell scripts. It offers many features that enhance productivity and ease of use for both beginners and experienced users. Here’s how to get started with PowerShell ISE.
Opening PowerShell ISE
Windows 10/11:
Press Win + R, type powershell_ise, and press Enter.
Alternatively, search for “Windows PowerShell ISE” in the Start menu and select it.
Windows 7/8:
Open the Start menu, go to All Programs > Windows PowerShell > Windows PowerShell ISE.
Components of PowerShell ISE
Script Pane:
The top pane where you write and edit your scripts. You can open multiple tabs for different scripts.
Console Pane:
The bottom pane where you can interactively run commands and see their output.
Output Pane:
Located within the Console Pane, it displays the output from the commands you run.
Command Add-On Pane:
A side pane (toggle with Ctrl + R) that helps you browse and insert cmdlets and their parameters.
Basic Operations in PowerShell ISE
Creating a Script:
Click File > New or press Ctrl + N to create a new script file.
#Write your script in the Script Pane. For example:
$name = “PowerShell User”
Write-Output “Hello, $name!”
Saving a Script:
Click File > Save or press Ctrl + S.
Choose a location and save the script ” firstscript.ps1″ extension.
Running a Script:
Click Run Script (green arrow) in the toolbar or press F5.
To run a selected portion of the script, highlight the text and press F8.
Debugging a Script:
Set breakpoints by clicking in the margin next to the line numbers or pressing F9 on the desired line.
Run the script with F5. Execution will pause at breakpoints, allowing you to inspect variables and step through the code.
Using the Console Pane:
You can execute commands directly in the Console Pane, just like in the standard PowerShell console.
Get-Process
IntelliSense:
PowerShell ISE provides IntelliSense, which offers command and parameter suggestions as you type, reducing syntax errors and speeding up script development.
Syntax Highlighting:
Keywords, cmdlets, variables, strings, and comments are color-coded to improve readability.
Snippet Support:
Use code snippets to quickly insert common constructs. Access snippets with Ctrl + J or via the context menu.
Customizing the Environment:
Adjust font size, colors, and other settings through Tools > Options.
Writing a Script:
# MyScript.ps1
$date = Get-Date
$message = “Hello, today’s date is $date”
Write-Output $message
Setting a Breakpoint:
Click on the margin next to the Write-Output $message line to set a breakpoint.
Running and Debugging:
Press F5 to run the script. It will pause at the breakpoint.
Hover over $message to inspect its value.
Press F10 to step over to the next line or F5 to continue execution.
Saving and Exiting:
Save your script with Ctrl + S.
Close PowerShell ISE with Alt + F4 or File > Exit.
Using the Command Add-On Pane:
Browse available cmdlets, view their parameters, and insert them into your script with ease.
Example Workflow in PowerShell ISE
Running PowerShell Commands and Scripts
PowerShell commands and scripts allow users to automate tasks, manage systems, and perform a wide range of administrative functions. Here’s a guide on how to run commands and scripts effectively.
Running Commands in the PowerShell Console
Opening PowerShell:
Windows: Press Win + R, type powershell, and press Enter.
Basic Commands:
Enter the command at the prompt and press Enter.
# Get all the process
Get-Process
Using Parameters:
Many commands (cmdlets) have parameters to modify their behavior.
powershell
#get the process in the ame of explorer
Get-Process -Name “explorer”
Using Aliases:
PowerShell has aliases for commonly used commands to simplify typing.
# Full cmdlet for geting child item
Get-ChildItem
# Alias for the same
ls
Running Scripts in the PowerShell Console
Creating a Script:
Write your PowerShell commands in a text file with a .ps1 extension.
# ExampleScript.ps1
Write-Output “Hello, Zibeet Tutor!”
Setting Execution Policy:
To run scripts, you might need to adjust the execution policy. Open PowerShell as Administrator and run:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Running a Script:
Navigate to the script’s directory and run the script using .\ notation.
cd C:\Script
.\ExampleScript.ps1
Using the -File Parameter:
Running Scripts in PowerShell ISE
Opening PowerShell ISE:
Press Win + R, type powershell_ise, and press Enter.
Creating and Editing a Script:
Create a new script by clicking File > New or pressing Ctrl + N.
Write your script in the Script Pane.
# ExampleScript.ps1
Write-Output “Hello, PowerShell ISE!”
Running a Script:
Run the entire script by clicking the green Run Script button or pressing F5.
To run a selected portion of the script, highlight the text and press F8.
Debugging a Script:
Set breakpoints by clicking in the margin next to the line numbers.
Use F5 to start debugging, and F10 or F11 to step through the code.
Advanced Script Execution
Using Parameters in Scripts:
Scripts can accept parameters to make them more flexible.
# ExampleScript.ps1
param (
[string]$Name = “PowerShell User”
)
Write-Output “Hello, $Name!”#Run the script with parameters:
.\ExampleScript.ps1 -Name “John Doe”