From 8c82bdbb72cc8aa1aa23cc04c69bf4d46433fdad Mon Sep 17 00:00:00 2001 From: deadlydog Date: Sun, 13 Oct 2024 13:00:24 -0600 Subject: [PATCH 1/2] tip: Add Type Accelerators tip chore: Generate updated PowerShelltips.json file --- .cspell.json | 4 +- ...instead-of-fully-qualified-class-names.ps1 | 55 +++++++++++++++++++ src/tiPS/PowerShellTips.json | 38 +++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/PowerShellTips/2024-10-13-use-type-accelerators-instead-of-fully-qualified-class-names.ps1 diff --git a/.cspell.json b/.cspell.json index 500223a..f030674 100644 --- a/.cspell.json +++ b/.cspell.json @@ -30,8 +30,10 @@ "madrapps", // GitHub action author "netstandard", // .NET Standard project target "nunit", // .NET unit testing framework - "pwsh", // PowerShell Core "PowerShelldle", // PowerShell game + "pscredential", // PowerShell type + "pscustomobject", // PowerShell type + "pwsh", // PowerShell Core "Wordle" // Game ], "ignoreRegExpList": [ diff --git a/src/PowerShellTips/2024-10-13-use-type-accelerators-instead-of-fully-qualified-class-names.ps1 b/src/PowerShellTips/2024-10-13-use-type-accelerators-instead-of-fully-qualified-class-names.ps1 new file mode 100644 index 0000000..f5ea111 --- /dev/null +++ b/src/PowerShellTips/2024-10-13-use-type-accelerators-instead-of-fully-qualified-class-names.ps1 @@ -0,0 +1,55 @@ +$tip = [tiPS.PowerShellTip]::new() +$tip.CreatedDate = [DateTime]::Parse('2024-10-13') +$tip.Title = 'Use Type Accelerators instead of fully qualified class names' +$tip.TipText = @' +Type accelerators allow us to use a short alias for a .NET type, instead of typing the fully qualified class name. There are many built-in type accelerators, such as: + +[int] instead of [System.Int32] +[string] instead of [System.String] +[datetime] instead of [System.DateTime] +[array] instead of [System.Array] +[xml] instead of [System.Xml.XmlDocument] +[regex] instead of [System.Text.RegularExpressions.Regex] +[pscredential] instead of [System.Management.Automation.PSCredential] +[pscustomobject] instead of [System.Management.Automation.PSCustomObject] + +See the About_Type_Accelerators help topic for a full list of built-in type accelerators. + +We can also create type accelerators for our own custom types, or existing .NET types that do not have a type accelerator. +'@ +$tip.Example = @' +# These lines are equivalent: +[int] $myNumber = 42 +[System.Int32] $myNumber = 42 + +# These lines are equivalent: +[string]::IsNullOrWhiteSpace($someString) +[System.String]::IsNullOrWhiteSpace($someString) + +# Create a type accelerator for an existing .NET class, and then use it to create a new instance. +$TypeAcceleratorsClass = [PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators') +$TypeAcceleratorsClass::Add('TCP','System.Net.Sockets.TCPClient') +$tcpClient = [TCP]::new('localhost', 80) + +# Show all available type accelerators. +[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get +'@ +$tip.Urls = @( + 'https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_type_accelerators' + 'https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes#exporting-classes-with-type-accelerators' + 'https://4sysops.com/archives/using-powershell-type-accelerators/' +) +$tip.Category = [tiPS.TipCategory]::Syntax # Community, Editor, Module, NativeCmdlet, Performance, Security, Syntax, Terminal, or Other. +$tip.Author = 'Daniel Schroeder (deadlydog)' # Optional. Get credit for your tip. e.g. 'Daniel Schroeder (deadlydog)'. +#$tip.ExpiryDate = [DateTime]::Parse('2024-10-30') # Optional. If the tip is not relevant after a certain date, set the expiration date. e.g. Announcing a conference or event. + +# Category meanings: +# Community: Social events and community resources. e.g. PowerShell Summit, podcasts, etc. +# Editor: Editor tips and extensions. e.g. VSCode, ISE, etc. +# Module: Modules and module tips. e.g. PSScriptAnalyzer, Pester, etc. +# NativeCmdlet: Native cmdlet tips. e.g. Get-Process, Get-ChildItem, Get-Content, etc. +# Performance: Tips to improve runtime performance. e.g. foreach vs ForEach-Object, ForEach-Object -Parallel, etc. +# Security: Security tips. e.g. ExecutionPolicy, Constrained Language Mode, passwords, etc. +# Syntax: Syntax tips. e.g. splatting, pipeline, etc. +# Terminal: Terminal shortcuts and tips. e.g. PSReadLine, Windows Terminal, ConEmu, etc. +# Other: Tips that don't fit into any of the other categories. diff --git a/src/tiPS/PowerShellTips.json b/src/tiPS/PowerShellTips.json index 1e4b5fa..c90cdf6 100644 --- a/src/tiPS/PowerShellTips.json +++ b/src/tiPS/PowerShellTips.json @@ -700,5 +700,43 @@ "Category": 6, "ExpiryDate": "9999-12-31T23:59:59.9999999", "Author": "Daniel Schroeder (deadlydog)" + }, + { + "CreatedDate": "2024-09-23T00:00:00", + "Title": "Drag File or Folder into PowerShell to get the path", + "TipText": "You can drag a file or folder from the File Explorer into PowerShell to get the full path.", + "Example": "", + "Urls": [ + "https://lazyadmin.nl" + ], + "Category": 7, + "ExpiryDate": "9999-12-31T23:59:59.9999999", + "Author": "Rudy Mens (LazyAdmin)" + }, + { + "CreatedDate": "2024-09-23T00:00:00", + "Title": "Press Tab twice to view all parameters, properties and possibilities", + "TipText": "Add `Set-PSReadLineKeyHandler -Key Tab -Function Complete` to your PowerShell profile. This way you can press the Tab key twice after a command to view all possible parameters, properties, and possibilities.\r\n\r\nThe double tab method is similar to the normal tab completion, except it will show you all of the options at once instead of just one option at a time.\r\n\r\nNote: Requires the `PSReadLine` module, which is included in PowerShell 5.1 and newer.", + "Example": "# Open your PowerShell Profile:\r\nnotepad $profile\r\n\r\n# Add the following line:\r\nSet-PSReadLineKeyHandler -Key Tab -Function Complete", + "Urls": [ + "https://lazyadmin.nl/powershell/powershell-cheat-sheet/#good-to-know" + ], + "Category": 7, + "ExpiryDate": "9999-12-31T23:59:59.9999999", + "Author": "Rudy Mens (LazyAdmin)" + }, + { + "CreatedDate": "2024-10-13T00:00:00", + "Title": "Use Type Accelerators instead of fully qualified class names", + "TipText": "Type accelerators allow us to use a short alias for a .NET type, instead of typing the fully qualified class name. There are many built-in type accelerators, such as:\r\n\r\n[int] instead of [System.Int32]\r\n[string] instead of [System.String]\r\n[datetime] instead of [System.DateTime]\r\n[array] instead of [System.Array]\r\n[xml] instead of [System.Xml.XmlDocument]\r\n[regex] instead of [System.Text.RegularExpressions.Regex]\r\n[pscredential] instead of [System.Management.Automation.PSCredential]\r\n[pscustomobject] instead of [System.Management.Automation.PSCustomObject]\r\n\r\nSee the About_Type_Accelerators help topic for a full list of built-in type accelerators.\r\n\r\nWe can also create type accelerators for our own custom types, or existing .NET types that do not have a type accelerator.", + "Example": "# These lines are equivalent:\r\n[int] $myNumber = 42\r\n[System.Int32] $myNumber = 42\r\n\r\n# These lines are equivalent:\r\n[string]::IsNullOrWhiteSpace($someString)\r\n[System.String]::IsNullOrWhiteSpace($someString)\r\n\r\n# Create a type accelerator for an existing .NET class, and then use it to create a new instance.\r\n$TypeAcceleratorsClass = [PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')\r\n$TypeAcceleratorsClass::Add('TCP','System.Net.Sockets.TCPClient')\r\n$tcpClient = [TCP]::new('localhost', 80)\r\n\r\n# Show all available type accelerators.\r\n[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get", + "Urls": [ + "https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_type_accelerators", + "https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes#exporting-classes-with-type-accelerators", + "https://4sysops.com/archives/using-powershell-type-accelerators/" + ], + "Category": 6, + "ExpiryDate": "9999-12-31T23:59:59.9999999", + "Author": "Daniel Schroeder (deadlydog)" } ] From eb0acc98022461386a1bb3349c9623e9f1e1ed22 Mon Sep 17 00:00:00 2001 From: deadlydog Date: Sun, 13 Oct 2024 13:04:48 -0600 Subject: [PATCH 2/2] chore: Fix spellcheck --- .cspell.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.cspell.json b/.cspell.json index f030674..9f35a39 100644 --- a/.cspell.json +++ b/.cspell.json @@ -20,6 +20,7 @@ "ignoreWords": [ "Codespace", // GitHub Codespaces "Codespaces", // GitHub Codespaces + "datetime", // PowerShell type "dawidd", // GitHub action author "deadlydog", // Username "devcontainer", // VS Code devcontainer