{"id":677,"date":"2023-09-15T13:48:00","date_gmt":"2023-09-15T13:48:00","guid":{"rendered":"http:\/\/draith.com\/?p=677"},"modified":"2024-03-01T01:54:52","modified_gmt":"2024-03-01T01:54:52","slug":"hidden-gems-in-powershell-7","status":"publish","type":"post","link":"http:\/\/draith.com\/?p=677","title":{"rendered":"Hidden Gems in PowerShell 7"},"content":{"rendered":"\n<p>  PowerShell 7 introduces several lesser-known features that can significantly enhance your scripting prowess. Let&#8217;s dive into these hidden gems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Ternary Operator for Concise Conditional Logic<\/h3>\n\n\n\n<p>  PowerShell 7 brings the ternary operator (<code>?:<\/code>), a shorthand for simple <code>if-else<\/code> statements, allowing for more concise and readable code.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;PowerShell&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$result = ($value -eq 10) ? &quot;Equal to 10&quot; : &quot;Not equal to 10&quot;<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Pipeline Parallelization with <code>ForEach-Object -Parallel<\/code><\/h3>\n\n\n\n<p>  The <code>-Parallel<\/code> parameter in <code>ForEach-Object<\/code> can dramatically improve performance by executing script blocks in parallel. Note that it requires the use of the <code>-ThrottleLimit<\/code> parameter to control the number of concurrent threads.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">1..50 | ForEach-Object -Parallel { $_ * 2 } -ThrottleLimit 10<\/pre><\/div>\n\n\n\n<p>  <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Simplified Error Viewing with <code>$ErrorView<\/code> and <code>Get-Error<\/code><\/h3>\n\n\n\n<p>  PowerShell 7 introduces a new view for error messages through the <code>$ErrorView<\/code> variable, which can be set to <code>ConciseView<\/code> for a more streamlined error display. Additionally, <code>Get-Error<\/code> provides detailed error information, perfect for troubleshooting.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$ErrorView = 'ConciseView'\nGet-Error<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Null Conditional Operators for Handling <code>$null<\/code><\/h3>\n\n\n\n<p>  The null conditional operators <code>?.<\/code> and <code>?[]<\/code> provide a safe way to access properties and methods or index into arrays when there&#8217;s a possibility of <code>$null<\/code> values, preventing unnecessary errors.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$obj = $null\n$name = $obj?.Name  # Returns $null without throwing an error\n$value = $array?[0] # Safely attempts to access the first element\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The <code>switch<\/code> Statement Enhancements<\/h3>\n\n\n\n<p>PowerShell 7 enhances the <code>switch<\/code> statement with the <code>-Regex<\/code> and <code>-File<\/code> options, allowing pattern matching against regex expressions and simplifying file content parsing.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">switch -Regex ($inputString) {\n    'error' { Write-Output 'Error found' }\n    'warning' { Write-Output 'Warning found' }\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Coalescing Operators for Default Values<\/h3>\n\n\n\n<p>The null coalescing operators <code>??<\/code> and <code>??=<\/code> simplify the process of providing default values for potentially <code>$null<\/code> variables, reducing the need for verbose <code>if<\/code> statements.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$name = $null\n$displayName = $name ?? 'Default Name'<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Automatic Unwrapping of Single-Element Arrays<\/h3>\n\n\n\n<p>A subtle but handy feature; when a command or expression returns an array with a single element, PowerShell 7 automatically unwraps it, eliminating the need for manual indexing to access the single item.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enhanced JSON Handling with <code>ConvertFrom-Json<\/code> and <code>ConvertTo-Json<\/code><\/h3>\n\n\n\n<p>Improvements to <code>ConvertFrom-Json<\/code> and <code>ConvertTo-Json<\/code> cmdlets include better depth handling and the ability to work with <code>PSCustomObject<\/code> instances, streamlining JSON serialization and deserialization.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$json = '{&quot;name&quot;: &quot;PowerShell&quot;, &quot;version&quot;: 7}'\n$obj = $json | ConvertFrom-Json<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Invoke DSC Resources Directly from PowerShell 7<\/h3>\n\n\n\n<p>Directly invoking Desired State Configuration (DSC) resources within PowerShell 7 scripts bridges traditional configuration management with modern PowerShell scripting, enhancing automation capabilities.<\/p>\n\n\n\n<p>There ya go!  Hope you find something in here that makes coding a bit more fun\/easy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell 7 introduces several lesser-known features that can significantly enhance your scripting prowess. Let&#8217;s dive into these hidden gems. Ternary Operator for Concise Conditional Logic PowerShell 7 brings the ternary operator (?:), a shorthand for simple if-else statements, allowing for more concise and readable code. Pipeline Parallelization with ForEach-Object -Parallel The -Parallel parameter in ForEach-Object &hellip; <a href=\"http:\/\/draith.com\/?p=677\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Hidden Gems in PowerShell 7&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[16],"class_list":["post-677","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-powershell"],"_links":{"self":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=677"}],"version-history":[{"count":1,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/677\/revisions"}],"predecessor-version":[{"id":678,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/677\/revisions\/678"}],"wp:attachment":[{"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=677"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}