Power-Shell scripts have a . ps1 file extension.
Basic Syntax¶
Write-Host "I Learning Shell" -NoNewline
"text to send " | Out-File texto.txt
$val = "variable"
$extra = $val.GetType()
$MyList = @('first', 'second', 'etc')
if ( 3 -eq 3) { Write-Host "3 equal 3 "}
elseif () { Write-Host "other"}
else { Write-Host "3 not equal 3"}
switch ('1'){
'1' { Write-Host "test"; break }
Default { Write-Host "Default Write "}
}
Functions Basic Syntax¶
function Global:Get-MachineInfo {
<#
.SYNOPSIS
Sharing synopsis for this function
.DESCRIPTION
This an description
.PARAMETER make
This an parameter of write
.EXAMPLE
Get-MachineInfo -make "oeeee"
#>
write-host "write on the function"
}
# special parameters
function Global:Get-MachineInfoo {
param ([String[]]$make="simple", $otherMake) # parameters
Write-Host "other function $make"
}
# advanced Functions
(Get-Command -Name Get-MachineInfoo).Parameters.Keys
(Get-Command -Name Get-Service).Parameters.Keys
function Global:Get-MachineInfooo {
[CmdLetBinding()] # extras parameters
param ($make) # parameters
Write-Host "other function $make"
Write-Verbose "Sharing Information about my function"
}
Get-MachineInfo
Get-MachineInfoo -make "simple" # call parameters
Get-MachineInfooo -Verbose
# Help get-services -showWindow
# gci Function:\Get-MachineInfo |fl * --> get function info
# gci Function:\Get-MachineInfo |fl *
# adding whatIf & Confirmation Parameters
function Global:Get-MachineInf {
[CmdLetBinding(SupportsShouldProcess)]
param ([Parameter(Mandatory)] $make) #enter parameter
Write-Host "other function $make"
Write-Verbose "Sharing Information about my function"
}
Get-MachineInf
Docs¶
- official docs https://learn.microsoft.com/en-us/powershell/