What I learned about powershell in 2019
For ease of finding, I’m separated out powershell learnings from general trivia
Dec 20, 2019
#TodayILearned that -ExcludeProperty only works if used in conjunction with -property e.g.
get-AdUser mattypenny -properties * | select-object -Property * -ExcludeProperty userCertificate
#Powershell
Dec 3, 2019
#TodayILearned that to show all the commits (including messages) for a particular file you do
git log --follow -- <filename>
Dec 2, 2019
#TodayILearned that this inserts today’s date in vim
:put=strftime('%d %B %Y')
Nov 13, 2019
#TodayILearned that if you are daft enough to have a begin ‘bit’ without process and end ‘bits’ in a Powershell function, then it returns:
"Missing closing '}' in statement block or type definition."
It’s perhaps not the most useful error message for this particular case…
Oct 22, 2019
#TodayILearned this bit of Powershell strips out the ‘Registered Trade Mark’ (little R in a circle) symbol from a string.
In this case the string was the caption for the Windows OS in Server 2008).
[string]$RegisterMark = [char]174
$Caption = $Caption.replace($RegisterMark,'')
Oct 22, 2019
#TodayILearned how to handle quotes when generating Powershell
foreach ($Name in "spider stacy","cait o'riordan") {
$Name = $Name -replace "'","''"
$String = @"
get-ADUser -filter {Name -eq '$Name'}
"@
Invoke-Expression -Command $String | select name
}
Oct 17, 2019
#TodayILearned that @adbertram has a marvellous function for doing dynamic ValidateSets
Mastering PowerShell Parameter Validation with Dynamic Methods
Oct 15, 2019
#TodayILearned that ‘globalisation’ is spelt differently in different countries
(via @DoctorDns in the first #Powershell Conference book….which is fab)
https://lexico.com/en/definition/globalization
Oct 14, 2019
#TodayILearned to do Ctrl-Alt-Del on a Remote Desktop
- Start, then ‘osk’ for on-screen keyboard
- hold Ctrl-Alt on the ‘off-screen keyboard’
- click on Del on the on-screen one
How to CTRL + ALT + DEL in Remote Desktop on Windows - Guiding Tech
Oct 9, 2019
#TodayILearned that you can use a Powershell class to implement a dynamic ValidateSet for a parameter
My excitement at discovering this will possibly not be shared by everyone on Twitter
about Functions Advanced Parameters - PowerShell | Microsoft Learn
Jun 20, 2019
#TodayILearned these are equivalent, I think
Invoke-Pester -script .\diagnostics\simple\Server1.tests.ps1 -PassThru | select -ExpandProperty TestResult | select Result, Name
and
Invoke-OperationValidation -testFilePath .\diagnostics\simple\Server1.tests.ps1 -IncludePesterOutput
Jun 19, 2019
#TodayILearned that there is an @ansible module to apply windows updates (following on from @jonathanmedd’s talk at @PSSouthampton last night)
https://docs.ansible.com/ansible/latest/modules/win_updates_module.html#win-updates-module
Jun 5, 2019
#TodayILearned that you can pass all the parameters from one Powershell function to another in the built-in $PSBoundParameters variable
Feb 27, 2019
Replying to
I never knew you could do this either:
invoke-pester -script testscript.tests.ps1 *> c:\temp\TestResults.txt
The star re-directs all streams
Feb 27, 2019
#TodayILearned that if you want to re-direct the screen output from @PSPester to a file, then you use a ‘6’
invoke-pester -script testscript.tests.ps1 6> c:\temp\TestResults.txt
Feb 26, 2019
#TodayILearned that VS @Code has really nice diff functionality.
Ctrl-Shift-P then File: Compare Active File With…
Jan 1, 2019
#TodayILearned that git status -s gives a more usefully formatted view of changes in a repository
Dec 11, 2018
#TodayILearnedthat in Powershell to replace ‘carriage return’ (I think…it renders as \r in my setup), you have to put the `r in double quotes not single quotes
$BodyText = $BodyText.replace("`r"," “)
Nov 8, 2018
#TodayILearned that you can use [ValidateSet] with powershell variables other than parameters
(I’m hoping this will, in this case, guard against typo’s later in the code)
Nov 8, 2018
#TodayILearned you can set a VS @code setting as follows:
“files.defaultlanguage”: “powershell”
….to have new files default to Powershell
(via @vhusker )
jacobbenson.azurewebsites.net/index.php…
Oct 15, 2018
#TodayILearned that the little outline thing in VS @code is called a ‘minimap’ and to turn it off you do:
Ctrl-Shift-P,
View:Toggle Minimap
Oct 12, 2018
#TodayILearned that you can use -lt and -gt with letters.
i.e. to get names in the first half of the alphabet
$x | where name -lt ‘N’
(via @Idera_software ) From blog.idera.com
Sep 25, 2018
#TodayILearned that you can slide between the tabs in VS @code with this skinny semi-hidden slider
Jul 31, 2018
Trying to test if a bit of text contains a backtick in #Powershell, I couldn’t get -like “``” to work, but this seems to:
if ($Line.Contains('`'))
The bit in brackets is single quote, back-tick, single quote
Jun 22, 2018
#TodayILearned that typing ‘code’ in bash opens VS Code
May 29, 2018
#TodayILearned that in #Powershell on linux, although you can cd to a folder where the slashes go in the ‘wrong’ direction, you have to get the slashes ‘right’ for [http://System.IO.Path] to work
May 22, 2018
#TodayILearned (or more accurately, I think re-learned!) that the Powershell $Env:PSModulePath separators on Linux are colons not semi-colons
May 8, 2018
#TodayILearned Quote Cody Konior @codykonior
May 8, 2018
Super handy #powershell tip. When you pipe an object to Format-List it will cut arrays off and show then like Name : {A, B, C…}. You can actually stop that with $FormatEnumerationLimit = -1.
Apr 26, 2018
#TodayILearned that if you’re running #Powershell on @ubuntu you need to get the case ‘right’ in your $Env:PSModulePath
I guess because it’s a $Env variable the case sensitivity is determined by the Environment not the shell?
Apr 24, 2018
#TodayILearned that
Invoke-item .
….will bring up the file manager gui on @ubuntu powershell, as it would on windows
Apr 18, 2018
#TodayILearned that you have to do:
Install-WindowsFeature RSAT-AD-PowerShell
to get the ActiveDirectory module in Server2016
(via ntweekly.com/2016/07/0…)
Apr 9, 2018
#TodayILearned that managed service accounts don’t show up in Get-ADUser - you have to use Get-AdServiceAccount
#PowerShell
Mar 29, 2018
#TodayILearned that you have to stick a $ on the end of the Managed Service Account name for the @SQLServer installer to recognize it
It need a dollar, dollar a dollar is what it need
Mar 21, 2018
#TodayILearned that on older SQL installations which don’t have sqlps, you have to do:
add-pssnapin SQLServerCmdletSnapin100
and optionally
add-pssnapin SQLServerProviderSnapin100
#Powershell
Mar 1, 2018
#TodayILearned that
Where-Object : Cannot bind parameter ‘FilterScript’. Cannot convert the “Name” value of type “System.String” to type “System.Management.Automation.ScriptBlock”.
..typically means I’m running on an olde worlde version of Powershell
Feb 20, 2018
#TodayILearned the magic of ‘gk’ and ‘gj’, which go up and down within a wrapped line in vim
Feb 1, 2018
#TodayILearned that you can use a bar (or ‘pipe symbol’) to enter more than one ‘ex’ command in Vim i.e.
:%s/[‘’]/'/g | %s/SQLData/sql_data/g