Showing the current OS
Get-ComputerInfo
Commands
File System Commands
Showing the path of the current directory
Get-Location
Showing the content of the current directory
Get-ChildItem
The same recursively and with filter capability
The following command lists all files files matching the filter criteria inside the folder cypress recursively:
Get-ChildItem -Path "cypress" -Filter *cy.ts -Recurse
The same for the current path:
Get-ChildItem -Filter *cy.ts -Recurse
Other commands
Creating a new GUID
There are different ways to easily create a new GUID using PowerShell, here are two different ways to achieve it:
First example:
[guid]::NewGuid()
Second example:
New-Guid
Environment variables
Showing all environment variables
To show all environment variables in PowerShell, you can use the Get-ChildItem
command with the Env:
option as the following example shows:
Get-ChildItem Env:
Functions
The following example shows how you can declare and use a custom function in PowerShell:
function Get-Version {
$PSVersionTable.PSVersion
}
Get-Version
![](https://www.iteacorner.com/wp-content/uploads/2022/08/image-7.png)
You can also
Function with parameters
The following example shows a simple function CalculateY which is called and passed the value 10 to its variable X, which then calculates the square of that value and prints it out:
function CalculateY{
param(
[Int]$X
)
Write-Output "$($X * $X)"
}
CalculateY -X 10
![](https://www.iteacorner.com/wp-content/uploads/2022/08/image-8.png)
You can have the same with a slightly different syntax as follows:
function CalculateY($X){
Write-Output "$($X * $X)"
}
CalculateY -X 10
![](https://www.iteacorner.com/wp-content/uploads/2022/08/image-9.png)
Return value
The following example explains how the function can return a value and how that value can be used to do something with it, for example writing it into output:
function GetArray
{
$array = 1,2,3,4
return $array
}
$a = GetArray
Write-Output $a
This will produce the following output:
1 2 3 4 |
But in fact, the return keyword is not even necessary, you basically get the same result with the following code without the return keyword:
function GetArray
{
$array = 1,2,3,4
$array
}
$a = GetArray
Write-Output $a
Unexpected behavior with using Write-Output inside a function
Be aware that using Write-Output inside a function might cause unexpected behavior because Write-Output write to the same stream where the return value is written. The following example shows how having Write-Output inside a function can change your return value in an unexpected way:
Script:
function GetArray
{
$array = 1,2,3,4
Write-Output "xyz"
return $array
}
$a = GetArray
Write-Output $a
Write-Output $a
Output:
xyz 1 2 3 4 xyz 1 2 3 4 |
As you can see we call the function only once, but xyz is written out to output twice. This means that Write-Output directly effects the return value, meaning that the string xyz becomes part of the return value.
Here you can read more about this problem:
https://community.spiceworks.com/topic/2189940-powershell-write-output-in-function-with-return-value
PowerShell ISE
Commenting out
Commenting out a line:
![](https://www.iteacorner.com/wp-content/uploads/2022/08/image-10.png)
Commenting out a block of lines:
![](https://www.iteacorner.com/wp-content/uploads/2022/08/image-11.png)