diff options
author | Andrew Ryan Davis <AndrewDavis1191@gmail.com> | 2020-05-16 19:26:29 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-16 19:26:29 -0700 |
commit | 6151e30b6b7d069095611ceab616a04eed4b2782 (patch) | |
tree | ac06e3c99a79bb248778260dbe2cf47552f98ad1 /powershell.html.markdown | |
parent | f21a011eb4923c59343ec8f8dc1ab3166e9de6ba (diff) |
[powershell/en] Adding some things I find useful
Diffstat (limited to 'powershell.html.markdown')
-rw-r--r-- | powershell.html.markdown | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/powershell.html.markdown b/powershell.html.markdown index 5d74024d..75645bbb 100644 --- a/powershell.html.markdown +++ b/powershell.html.markdown @@ -55,6 +55,11 @@ The tutorial starts here: ```powershell # As you already figured, comments start with # +<# + Multi-line comments + like so +#> + # Simple hello world example: echo Hello world! # echo is an alias for Write-Output (=cmdlet) @@ -68,6 +73,8 @@ $aString="Some string" # Or like this: $aNumber = 5 -as [double] $aList = 1,2,3,4,5 +# Reverse an array *Note this is a mutation on the existing array +[array]::Reverse($aList) $anEmptyList = @() $aString = $aList -join '--' # yes, -split exists also $aHashtable = @{name1='val1'; name2='val2'} @@ -100,6 +107,10 @@ echo "Bound arguments in a function, script or code block: $PSBoundParameters" echo "Unbound arguments: $($Args -join ', ')." # More builtins: `help about_Automatic_Variables` +# Find the datatype of variables or properties you're working with +$true.GetType() +$aHashtable.name2.GetType() + # Inline another file (dot operator) . .\otherScriptName.ps1 @@ -184,7 +195,7 @@ Get-Process | Sort-Object ID -Descending | Select-Object -First 10 Name,ID,VM ` Get-EventLog Application -After (Get-Date).AddHours(-2) | Format-List # Use % as a shorthand for ForEach-Object -(a,b,c) | ForEach-Object ` +('a','b','c') | ForEach-Object ` -Begin { "Starting"; $counter = 0 } ` -Process { "Processing $_"; $counter++ } ` -End { "Finishing: $counter" } @@ -198,12 +209,13 @@ ps | Format-Table ID,Name,@{n='VM(MB)';e={'{0:n2}' -f ($_.VM / 1MB)}} -autoSize ### Functions # The [string] attribute is optional. -function foo([string]$name) { +# Function names should follow Verb-Noun convention +function Get-Foo([string]$name) { echo "Hey $name, have a function" } # Calling your function -foo "Say my name" +Get-Foo "Say my name" # Functions with named parameters, parameter attributes, parsable documentation <# @@ -283,7 +295,22 @@ Pop-Location # change back to previous working directory # Unblock a directory after download Get-ChildItem -Recurse | Unblock-File +# You can also pass arguments to a Function with a hash table +# This is called Splatting +# Normal Command +Export-Csv -InputObject $csv -Path 'c:\mypath' -Encoding UTF8 -NoTypeInformation +# With Splatting +$csvArguments = @{ + InputObject = $csv + Path = 'c:\mypath' + Encoding = 'UTF8' + NoTypeInformation = $true +} +Export-Csv @csvArguments + # Open Windows Explorer in working directory +Invoke-Item . +# Or the alias ii . # Any key to exit @@ -318,6 +345,7 @@ Interesting Projects * [PSGet](https://github.com/psget/psget) NuGet for PowerShell * [PSReadLine](https://github.com/lzybkr/PSReadLine/) A bash inspired readline implementation for PowerShell (So good that it now ships with Windows10 by default!) * [Posh-Git](https://github.com/dahlbyk/posh-git/) Fancy Git Prompt (Recommended!) +* [Oh-My-Posh](https://github.com/JanDeDobbeleer/oh-my-posh) * [PSake](https://github.com/psake/psake) Build automation tool * [Pester](https://github.com/pester/Pester) BDD Testing Framework * [Jump-Location](https://github.com/tkellogg/Jump-Location) Powershell `cd` that reads your mind |