This commit is contained in:
talon 2023-06-18 21:55:08 -06:00
parent 20966bc476
commit 1ec29a4d03

View file

@ -13,7 +13,7 @@ codium "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1"
# variables # variables
[PowerShell is as case-insensitive as possible.](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_case-sensitivity?view=powershell-7.3) This means `$MyInvocation` works just the same as `$mYinoCAtiOn` among other things. Coming from a Unix background and preferring some consistency I like to pretend it is case-sensitive so in this repo I'll strive for that but it's worth keeping in mind. Especially since the Microsoft docs use case a lot in variable and cmdlet names. [PowerShell is as case-insensitive as possible.](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_case-sensitivity?view=powershell-7.3) This means `$MyInvocation` works just the same as `$mYinoCAtiOn` among other things. Coming from a Unix background and preferring some consistency I like to pretend it is case-sensitive so in this repo I'll strive for that but it's worth keeping in mind. Especially since **the Microsoft docs use case a lot** in variable and cmdlet names.
Variables use `$` to distinguish themselves and can be set with `=` Variables use `$` to distinguish themselves and can be set with `=`
@ -55,7 +55,7 @@ param(
) )
``` ```
There are [a lot of different powers](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3) behind the square bracket syntax, [including custom validation](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3#validatescript-validation-attribute), but these are the basics. Parameters are not mandatory by default use `[Parameter(Mandatory)]` for that. The above param block could be used in a script like this: There are [a lot of different powers](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3) behind the square bracket syntax, [including custom validation](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3#validatescript-validation-attribute), but these are the basics. Use `[parameter(mandatory)]` to require one otherwise they are optional. The above param block could be used in a script like this:
```powershell ```powershell
if ($silent) { exit } if ($silent) { exit }
@ -80,17 +80,62 @@ Functions use `param` too (instead of parenthesis like other languages). The las
```powershell ```powershell
function My-Add { function My-Add {
param( param(
[Parameter(Mandatory)][int]$x, [parameter(mandatory)][int]$x,
[Parameter(Mandatory)][int]$y, [parameter(mandatory)][int]$y,
) )
$x + $y $x + $y
} }
``` ```
# For and ForEach # foreach
# Paths and Items Works on [arrays](https://learn.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-09?view=powershell-7.3#92-array-creation), which can be concatenated with `+` and `+=` by the way.
```powershell
$letterArray = "a","b","c","d"
foreach ($letter in $letterArray)
{
Write-Host $letter
}
```
And also works for results from `Get-ChildItem` like when looping through files in a folder.
```powershell
foreach ($file in Get-ChildItem)
{
if ($file.length -gt 100KB)
{
Write-Host $file
}
}
```
# items and paths
In PowerShell files and folders are called items. Some useful cmdlets for working with them are:
- [`New-Item`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-7.3#description) - create a file or folder
- [`Remove-Item`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-7.3#description) - delete a file or folder
- [Move, Invoke, Rename etc...](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/?view=powershell-7.3)
Here's how to loop through files in a folder using [`Get-ChildItem`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.3#description):
```powershell
foreach ($image Get-ChildItem $path -Include *.png) {
Write-Host $image.BaseName
}
```
There are also a handful of useful functions for dealing with path strings by the names of [[Verb]-Path](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/?view=powershell-7.3). Here are some examples:
```powershell
# Get parent folder
Split-Path -Parent $path
# Test if a file exists. For folder use `-PathType container`
Test-Path -Path $path -PathType leaf
```
# See Also # See Also