add a bit to the crash course
This commit is contained in:
parent
32cde1eba1
commit
df6299d804
|
@ -1,6 +1,6 @@
|
|||
# PowerShell Scripting Crash Course
|
||||
|
||||
There are a few different ways to get started.
|
||||
After having followed the [README.md](./README.md), `.ps1` scripts in the same folder as `$PROFILE.CurrentUserAllHosts` will be available in `$env:PATH` globally:
|
||||
|
||||
```powershell
|
||||
# Open the $PROFILE folder
|
||||
|
@ -11,14 +11,85 @@ notepad "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1"
|
|||
codium "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1"
|
||||
```
|
||||
|
||||
# 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.
|
||||
|
||||
Variables use `$` to distinguish themselves and can be set with `=`
|
||||
|
||||
```powershell
|
||||
$a_variable = "this one is a string"
|
||||
$interpolate_a_string = "like this: $a_variable"
|
||||
```
|
||||
|
||||
## booleans
|
||||
|
||||
There are a few constants to know: `$true`, `$false`, `$null`. Empty strings (`""`, `''`), `$null` and `0` evaluate as `$false`
|
||||
|
||||
# if and while
|
||||
|
||||
There are a handful of [comparison operators](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.3) which can be used with `if` or `while` like this:
|
||||
|
||||
```powershell
|
||||
if ($val -eq "y") {
|
||||
Write-Host "value is a y"
|
||||
}
|
||||
else {
|
||||
Write-Host "value is not a y"
|
||||
}
|
||||
|
||||
while ($num -lt 50) {
|
||||
$num = $num + 1
|
||||
}
|
||||
```
|
||||
|
||||
# param
|
||||
|
||||
# Read-Host
|
||||
At the beginning of scripts and functions `param` list can be specified like this
|
||||
|
||||
```powershell
|
||||
param(
|
||||
[string]$name = "Anonymous",
|
||||
[int]$age,
|
||||
[switch]$silent
|
||||
)
|
||||
```
|
||||
|
||||
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:
|
||||
|
||||
```powershell
|
||||
if ($silent) { exit }
|
||||
Write-Host "Hello $name"
|
||||
if ($age) {
|
||||
Write-Host "according to the -age you provided you are: $age"
|
||||
}
|
||||
```
|
||||
|
||||
And run like this:
|
||||
|
||||
```powershell
|
||||
name_and_age.ps1 -name "Rose" -age "27"
|
||||
```
|
||||
|
||||
# functions
|
||||
|
||||
Functions use `param` too (instead of parenthesis like other languages). The last value is the `return` result, (which you can also use to `return` early... or just regularly if you want)
|
||||
|
||||
```powershell
|
||||
function My-Add {
|
||||
param(
|
||||
[Parameter(Mandatory)][int]$x,
|
||||
[Parameter(Mandatory)][int]$y,
|
||||
)
|
||||
|
||||
$x + $y
|
||||
}
|
||||
```
|
||||
|
||||
# For and ForEach
|
||||
|
||||
# Paths and Items
|
||||
|
||||
# If, For and While
|
||||
|
||||
# See Also
|
||||
|
||||
- [About scripts (learn.microsoft.com)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scripts?view=powershell-7.3)
|
||||
|
|
|
@ -65,7 +65,7 @@ $env:PATH += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)"
|
|||
|
||||
## update_and_clean.ps1
|
||||
|
||||
With this I've also added a [`update_and_clean.ps1`](./update_and_clean.ps1) script (in the `$PROFILE` folder) that will update Windows as well as `scoop` if it's installed and then run `bleachbit --preset --clean` (also only if installed) **which depends on whatever you've last checked in the BleachBit UI... so run that and set it up first!** The script will also try to re-inkoke itself as Administrator if you're not in an elevated session already... which is usually. If you're familiar with Arch Linux you might say `yay`!
|
||||
With this I've also added a [`update_and_clean.ps1`](./update_and_clean.ps1) script (in the `$PROFILE` folder) that will update Windows as well as `scoop` if it's installed and then run `bleachbit --preset --clean` (also only if installed) **which depends on whatever you've last checked in the BleachBit UI... so run that and set it up first!** The script will also try to re-invoke itself as Administrator if you're not in an elevated session already... which is usually. If you're familiar with Arch Linux you might say `yay`!
|
||||
|
||||
Checkout the [Crash Course](./CrashCourse.md) for a terse guide on writing your own PowerShell scripts.
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ If (!(New-Object Security.Principal.WindowsPrincipal $(
|
|||
) {
|
||||
# and elevating hasn't already been tried
|
||||
if (!$elevated) {
|
||||
# attmempt to become Administrator by re-invoking this script
|
||||
# attempt to become Administrator by re-invoking this script
|
||||
Start-Process PowerShell -Verb RunAs -ArgumentList (
|
||||
'-noprofile -noexit -file "{0}" -elevated' -f ($MyInvocation.MyCommand.Definition)
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue