diff --git a/src/PowerShellTips/2023-09-23-define-hardcoded-array-items-on-new-lines-without-commas.ps1 b/src/PowerShellTips/2023-09-23-define-hardcoded-array-items-on-new-lines-without-commas.ps1 new file mode 100644 index 0000000..9e1f16b --- /dev/null +++ b/src/PowerShellTips/2023-09-23-define-hardcoded-array-items-on-new-lines-without-commas.ps1 @@ -0,0 +1,22 @@ +$tip = [tiPS.PowerShellTip]::new() +$tip.CreatedDate = [DateTime]::Parse('2023-09-23') +$tip.Title = 'Define hardcoded array items on new lines without commas' +$tip.TipText = @' +When defining hardcoded arrays with many values in PowerShell, you can define each item on a new line without commas. This makes it easier to read and maintain the array items, and you don't have to worry about commas when adding or rearranging items. + +If you choose to define multiple array values on a single line, then you will need a comma between each item on the line. +'@ +$tip.Example = @' +[string[]] $stringArray = @( + 'Notice that each item is on a new line without commas.' + 'This is much easier to read and maintain.' + 'And you can add comments to explain each item if needed.' # Like this. +) + +[int[]] $intArray = @(1, 2, 3) +'@ +$tip.Urls = @( + 'https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.3#create-an-array' +) +$tip.MinPowerShellVersion = '0.0' +$tip.Category = [tiPS.TipCategory]::Syntax # Community, Editor, Module, NativeCmdlet, Performance, Syntax, Terminal, or Other. diff --git a/src/tiPS/PowerShellTips.json b/src/tiPS/PowerShellTips.json index 7121acd..b01fcfc 100644 --- a/src/tiPS/PowerShellTips.json +++ b/src/tiPS/PowerShellTips.json @@ -199,6 +199,17 @@ "MinPowerShellVersion": "0.0", "Category": 4 }, + { + "CreatedDate": "2023-09-23T00:00:00", + "Title": "Define hardcoded array items on new lines without commas", + "TipText": "When defining hardcoded arrays with many values in PowerShell, you can define each item on a new line without commas. This makes it easier to read and maintain the array items, and you don't have to worry about commas when adding or rearranging items.\r\n\r\nIf you choose to define multiple array values on a single line, then you will need a comma between each item on the line.", + "Example": "[string[]] $stringArray = @(\r\n 'Notice that each item is on a new line without commas.'\r\n 'This is much easier to read and maintain.'\r\n 'And you can add comments to explain each item if needed.' # Like this.\r\n)\r\n\r\n[int[]] $intArray = @(1, 2, 3)", + "Urls": [ + "https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.3#create-an-array" + ], + "MinPowerShellVersion": "0.0", + "Category": 5 + }, { "CreatedDate": "2023-09-23T00:00:00", "Title": "Use $PSVersionTable to get PowerShell session info",