Powershell Basics
Describe Powershell.
The Microsoft-developed scripting language is straightforward and effective.
It is constructed using the.NET framework.
a strong automation tool that was created with system administrators in mind.
It is based on objects.
You may control the computer from the command line by using Windows PowerShell’s “cmdlets” of commands.
Its equivalent in Linux OS is known as a Bash scripting language.
How can I utilise PowerShell?
It functions as a command-line Shell as well as a scripting language.
It can communicate with a variety of technologies.
All types in the.NET framework are completely accessible through Windows PowerShell.
PowerShell is based on objects.
PowerShell has many front-end GUI interfaces that Microsoft created for its various applications.
It is more secure than running VBScript or other scripting languages.
It allows performing repetitive tasks more efficiently by combining multiple commands and by writing scripts. Suppose, a system administrator wants to create hundreds of active directory users, he can achieve this with the help of only some PowerShell cmdlets placed in a script.
Many complex and time-consuming configurations and tasks can be done in a second with simple cmdlets of PowerShell.
How to start the Windows PowerShell
PowerShell is available in all the latest version of Windows. We need to start PowerShell by following the given steps:
- Search for the Windows PowerShell. Select and Click.
Windows PowerShell ISE
The Microsoft Windows PowerShell ISE is a graphical user interface-based application and a default editor for Windows PowerShell. ISE stands for the Integrated Scripting Environment. It is an interface in which we can run commands and write, test, and debug PowerShell scripts without writing all the commands in the command-line interface.
The Integrated Scripting Environment (ISE) provides tab completion, multiline editing, syntax coloring, context-sensitive help, selective execution, and support for right-to-left languages.
The ISE window of PowerShell consists of following three panes:
Script Pane: This pane allows the users to create and run the scripts. A user can easily open, edit, and run the existing scripts in the script pane.
Output Pane: This pane displays the outputs of the scripts and the commands which are run by you. You can also clear and copy the contents in the Output pane.
Command Pane: This pane allows the users to write the commands. You can easily execute the single or multiple-line command in the command pane.
Enable PowerShell Scripts
When we start the PowerShell in a computer system, the default execution policy does not allow us to execute or run the scripts.
powershell scripting tutorial pdf
Powershell Unit Testing with Pester
![Powershell Unit Testing with Pester](https://www.news.itentertainment.org/wp-content/uploads/2022/12/unit-testing-powershell-code-with-pester-with-example-1024x507.png)
BeforeAll {
# your function
function Get-Planet ([string]$Name='*')
{
$planets = @(
@{ Name = 'Mercury' }
@{ Name = 'Venus' }
@{ Name = 'Earth' }
@{ Name = 'Mars' }
@{ Name = 'Jupiter' }
@{ Name = 'Saturn' }
@{ Name = 'Uranus' }
@{ Name = 'Neptune' }
) | foreach { [PSCustomObject]$_ }
$planets | where { $_.Name -like $Name }
}
}
# Pester tests
Describe 'Get-Planet' {
It "Given no parameters, it lists all 8 planets" {
$allPlanets = Get-Planet
$allPlanets.Count | Should -Be 8
}
Context "Filtering by Name" {
It "Given valid -Name '<Filter>', it returns '<Expected>'" -TestCases @(
@{ Filter = 'Earth'; Expected = 'Earth' }
@{ Filter = 'ne*' ; Expected = 'Neptune' }
@{ Filter = 'ur*' ; Expected = 'Uranus' }
@{ Filter = 'm*' ; Expected = 'Mercury', 'Mars' }
) {
param ($Filter, $Expected)
$planets = Get-Planet -Name $Filter
$planets.Name | Should -Be $Expected
}
It "Given invalid parameter -Name 'Alpha Centauri', it returns `$null" {
$planets = Get-Planet -Name 'Alpha Centauri'
$planets | Should -Be $null
}
}
}