# Computer Things - I Really Like Powershell (Highlights) ![rw-book-cover|256](https://buttondown.email/static/images/icons/[email protected]) ## Metadata **Review**:: [readwise.io](https://readwise.io/bookreview/26342793) **Source**:: #from/readwise **Zettel**:: #zettel/fleeting **Status**:: #x **Authors**:: [[Computer Things]] **Full Title**:: I Really Like Powershell **Category**:: #articles #readwise/articles **Category Icon**:: 📰 **URL**:: [buttondown.email](https://buttondown.email/hillelwayne/archive/i-really-like-powershell/) **Host**:: [[buttondown.email]] **Highlighted**:: [[2023-04-12]] **Created**:: [[2023-04-13]] ## Highlights - You can do the default thing and write `Get` and press tab to cycle through things that start with `Get`. But you can also write `*Content` and press tab to cycle through things that *end* with `Content`. Or you could do `*et-*nt` to get everything that ends with `nt` and has `et-` in the middle. And instead of pressing tab, you can press ctrl-space to navigate through all possible matches with the keyboard. ([View Highlight](https://read.readwise.io/read/01gxt9yksmpf411dw0sqr2629k)) ^507858175 - PowerShell has anonymous functions (blocks) and they’re objects too. ([View Highlight](https://read.readwise.io/read/01gxta0ta3a6wwa1c1nq35anbb)) ^507858464 ``` $timesince = {((Get-Date) - $_.LastWriteTime).days} gci | select FullName, $timesince ``` - [Select-Object](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object) maps over a collection of objects. `?` ([Where-Object](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object)) filters them and `%` ([Foreach-Object](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object)) runs a block on each object. ([View Highlight](https://read.readwise.io/read/01gxta3akc5tzdv7crttsbqj5s)) ^507858789 - So you can look for a string, filter by surrounding strings, and pass the matches along the pipeline without worrying about handling the context data passed too. ([View Highlight](https://read.readwise.io/read/01gxta62y385e3awm5vqv56jfj)) ^507859357 ``` select-string foo *.md -Context 2 | ? {$_.Context.DisplayPreContext -notmatch "^#"} | select Filename ``` - This makes it easy to send json in web requests ([View Highlight](https://read.readwise.io/read/01gxta90s69qj4t0vmq0g89405)) ^507859826 ``` convertTo-Json @{abc=@("efg","hij")} -Compress # {"abc":["efg","hij"]} ``` - Function parameters are surprisingly powerful ([View Highlight](https://read.readwise.io/read/01gxtag29s28qccj4p7059zwgk)) ^507864103 [Functions Advanced Parameters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters) - You can even create “parameter mixins” for functions; powershell provides the [SupportsShouldProcess](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_cmdletbindingattribute?view=powershell-7.2#supportsshouldprocess) which automatically adds `-WhatIf` and `-Confirm` to the function’s logic. ([View Highlight](https://read.readwise.io/read/01gxtac0ps56fxx9hep6a7etc6)) ^507860032 - People have told me that Powershell is a light wrapper around the .NET framework, which I know basically nothing about but you can use .NET types and methods in powershell scripts. For example, you can call `[math]::floor($x)`. I was once able to use `[System.Drawing.Bitmap]::FromFile` to embed a ton of PNGs into svgs. ([View Highlight](https://read.readwise.io/read/01gxtahz9g6mmssd3s9dxrrk5d)) ^507864503 - `help foo -ShowWindow` opens the reference in a small window with search and scrollbars. ([View Highlight](https://read.readwise.io/read/01gxtapgdp1pfr52gyt5ffe47t)) ^507864649 - [Format-Table](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-table) `-autosize` formats the output in a way that takes your viewport width into account. ([View Highlight](https://read.readwise.io/read/01gxtan28dk5t6vq4g02p2g8z5)) ^507864604 - ![](https://buttondown.imgix.net/images/c94ce77b-aa0d-4980-9e61-4f5c7247a195.png?w=960&fit=max) ([View Highlight](https://read.readwise.io/read/01gxtantcqs5zpjhjzqwzefnsb)) ^507864631 Out-Gridview