Skip to content

Commit

Permalink
Merge pull request #67 from deadlydog/Tip-TypeAccelerators
Browse files Browse the repository at this point in the history
tip: Add Type Accelerators tip
  • Loading branch information
deadlydog authored Oct 13, 2024
2 parents 7372cd4 + eb0acc9 commit 3d1d2d0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ignoreWords": [
"Codespace", // GitHub Codespaces
"Codespaces", // GitHub Codespaces
"datetime", // PowerShell type
"dawidd", // GitHub action author
"deadlydog", // Username
"devcontainer", // VS Code devcontainer
Expand All @@ -30,8 +31,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": [
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
38 changes: 38 additions & 0 deletions src/tiPS/PowerShellTips.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
]

0 comments on commit 3d1d2d0

Please sign in to comment.