{"id":71882,"date":"2015-07-28T00:01:00","date_gmt":"2015-07-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/07\/28\/one-of-my-favorite-powershell-functions\/"},"modified":"2019-02-18T09:46:49","modified_gmt":"2019-02-18T16:46:49","slug":"one-of-my-favorite-powershell-functions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/one-of-my-favorite-powershell-functions\/","title":{"rendered":"One of My Favorite PowerShell Functions"},"content":{"rendered":"<p><strong>Summary<\/strong><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, discusses one of his favorite functions:&nbsp;<\/span><b style=\"font-size:12px\">Get-EnumValues.<\/b><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I am working on my presentation for <a href=\"http:\/\/atltechstravaganza.com\/\" target=\"_blank\">Atlanta TechStravaganza 2015<\/a>, which will happen on August 21. It will be a really cool event, and the Scripting Wife and I are looking forward to attending it. Unfortunately we were unable to attend last year due to a scheduling conflict, so we are really looking forward to this year. It is always a great event with top-flight speakers.<\/p>\n<p>Anyway, I am still messing around with my Windows PowerShell profile&#8230;<\/p>\n<p>One of my favorite Windows PowerShell functions is one I wrote a while back. I call it the <b>Get-EnumValues<\/b> function. The reason I like it is because it will look up a .NET enum class and display the enum name and the enum value. This is cool because when I look up enums on the MSDN website, the enum names are listed, but not the numeric values&mdash;and sometimes I like to use the enum numeric value instead of the enum name.<\/p>\n<p>Here is an example that uses the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.management.automation.actionpreference(v=vs.85).aspx\" target=\"_blank\">System.Management.Automation.ActionPreference<\/a> enumeration. It is used to tell a Windows PowerShell cmdlet that is using the standard <b>&ndash;ErrorAction<\/b> parameter how to continue when an error arises. MSDN lists the names, but it does not list the numeric values.<\/p>\n<p>I have seen something like the following:<\/p>\n<p style=\"margin-left:30px\">Stop-Process &ndash;name Notepad &ndash;ErrorAction 0<\/p>\n<p>But there is no documentation that tells me what a 0 means. By using my <b>Get-EnumValue<\/b> function, I can easily find what a 0 means (or what a 5 means):<\/p>\n<p><span style=\"font-size:12px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-7-28-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-7-28-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><br \/><\/span><\/p>\n<p><span style=\"font-size:12px\">By directly using the enum numeric value, I make it quicker to work from the Windows PowerShell command line. It is not easier to read, but readability is not the major issue when working interactively from the Windows PowerShell console. (However, readability is a major issue when writing a Windows PowerShell script.)<\/span><\/p>\n<p>Here is my <b>Get-EnumValues<\/b> function:<\/p>\n<p style=\"margin-left:30px\">Function get-enumValues<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;# get-enumValues -enum &quot;System.Diagnostics.Eventing.Reader.StandardEventLevel&quot;<\/p>\n<p style=\"margin-left:30px\">Param([string]$enum)<\/p>\n<p style=\"margin-left:30px\">$enumValues = @{}<\/p>\n<p style=\"margin-left:30px\">[enum]::getvalues([type]$enum) |<\/p>\n<p style=\"margin-left:30px\">ForEach-Object {<\/p>\n<p style=\"margin-left:30px\">$enumValues.add($_, $_.value__)<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">$enumValues<\/p>\n<p style=\"margin-left:30px\">}&nbsp;<\/p>\n<p style=\"margin-left:30px\">And here is the alias I created for that function:<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name gev -Value Get-EnumValues | out-null<\/p>\n<p style=\"margin-left:30px\">Here is my complete profile as shown in the Windows PowerShell console:<\/p>\n<p style=\"margin-left:30px\">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p style=\"margin-left:30px\">#<\/p>\n<p style=\"margin-left:30px\"># PowerShell console profile<\/p>\n<p style=\"margin-left:30px\"># ed wilson, msft<\/p>\n<p style=\"margin-left:30px\">#<\/p>\n<p style=\"margin-left:30px\"># NOTES: contains five types of things: aliases, functions, psdrives,<\/p>\n<p style=\"margin-left:30px\"># variables and commands.<\/p>\n<p style=\"margin-left:30px\"># version 1.2<\/p>\n<p style=\"margin-left:30px\"># 7\/27\/2015<\/p>\n<p style=\"margin-left:30px\"># HSG 7-28-2015<\/p>\n<p style=\"margin-left:30px\">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p style=\"margin-left:30px\">#Aliases<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name ep -Value edit-profile | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name tch -Value Test-ConsoleHost | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name gfl -Value Get-ForwardLink | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name gwp -Value Get-WebPage | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name rifc -Value Replace-InvalidFileCharacters | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name gev -Value Get-EnumValues | out-null<\/p>\n<p style=\"margin-left:30px\">#Variables<\/p>\n<p style=\"margin-left:30px\">New-Variable -Name doc -Value &quot;$home\\documents&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; -Description &quot;My documents library. Profile created&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; -Option ReadOnly -Scope &quot;Global&quot;<\/p>\n<p style=\"margin-left:30px\">if(!(Test-Path variable:backupHome))<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;new-variable -name backupHome -value &quot;$doc\\WindowsPowerShell\\profileBackup&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -Description &quot;Folder for profile backups. Profile created&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -Option ReadOnly -Scope &quot;Global&quot;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">#PS_Drives<\/p>\n<p style=\"margin-left:30px\">New-PSDrive -Name Mod -Root ($env:PSModulePath -split &#039;;&#039;)[0] `<\/p>\n<p style=\"margin-left:30px\">&nbsp;-PSProvider FileSystem | out-null<\/p>\n<p style=\"margin-left:30px\">#Functions<\/p>\n<p style=\"margin-left:30px\">Function Edit-Profile<\/p>\n<p style=\"margin-left:30px\">{ ISE $profile }<\/p>\n<p style=\"margin-left:30px\">Function Test-ConsoleHost<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;if(($host.Name -match &#039;consolehost&#039;)) {$true}<\/p>\n<p style=\"margin-left:30px\">&nbsp;Else {$false}&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function Replace-InvalidFileCharacters<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param ($stringIn,<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $replacementChar)<\/p>\n<p style=\"margin-left:30px\">&nbsp;# Replace-InvalidFileCharacters &quot;my?string&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;# Replace-InvalidFileCharacters (get-date).tostring()<\/p>\n<p style=\"margin-left:30px\">&nbsp;$stringIN -replace &quot;[$( [System.IO.Path]::GetInvalidFileNameChars() )]&quot;, $replacementChar<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function Get-TranscriptName<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$date = Get-Date -format s<\/p>\n<p style=\"margin-left:30px\">&nbsp; &quot;{0}.{1}.{2}.txt&quot; -f &quot;PowerShell_Transcript&quot;, $env:COMPUTERNAME,<\/p>\n<p style=\"margin-left:30px\">&nbsp; (rifc -stringIn $date.ToString() -replacementChar &quot;-&quot;) }<\/p>\n<p style=\"margin-left:30px\">Function Get-WebPage<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param($url)<\/p>\n<p style=\"margin-left:30px\">&nbsp;# Get-WebPage -url (Get-CmdletFwLink get-process)<\/p>\n<p style=\"margin-left:30px\">&nbsp;(New-Object -ComObject shell.application).open($url)<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function Get-ForwardLink<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param($cmdletName)<\/p>\n<p style=\"margin-left:30px\">&nbsp;# Get-WebPage -url (Get-CmdletFwLink get-process)<\/p>\n<p style=\"margin-left:30px\">&nbsp;(Get-Command $cmdletName).helpuri<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function BackUp-Profile<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param([string]$destination = $backupHome)<\/p>\n<p style=\"margin-left:30px\">&nbsp; if(!(test-path $destination))<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; {New-Item -Path $destination -ItemType directory -force | out-null}<\/p>\n<p style=\"margin-left:30px\">&nbsp; $date = Get-Date -Format s<\/p>\n<p style=\"margin-left:30px\">&nbsp; $backupName = &quot;{0}.{1}.{2}.{3}&quot; -f $env:COMPUTERNAME, $env:USERNAME,<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; (rifc -stringIn $date.ToString() -replacementChar &quot;-&quot;),<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; (Split-Path -Path $PROFILE -Leaf)<\/p>\n<p style=\"margin-left:30px\">&nbsp;copy-item -path $profile -destination &quot;$destination\\$backupName&quot; -force<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function get-enumValues<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;# get-enumValues -enum &quot;System.Diagnostics.Eventing.Reader.StandardEventLevel&quot;<\/p>\n<p style=\"margin-left:30px\">Param([string]$enum)<\/p>\n<p style=\"margin-left:30px\">$enumValues = @{}<\/p>\n<p style=\"margin-left:30px\">[enum]::getvalues([type]$enum) |<\/p>\n<p style=\"margin-left:30px\">ForEach-Object {<\/p>\n<p style=\"margin-left:30px\">$enumValues.add($_, $_.value__)<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">$enumValues<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">#Commands<\/p>\n<p style=\"margin-left:30px\">Set-Location c:\\<\/p>\n<p style=\"margin-left:30px\">If(tch) {Start-Transcript -Path (Join-Path -Path `<\/p>\n<p style=\"margin-left:30px\">&nbsp;$doc -ChildPath $(Get-TranscriptName))}<\/p>\n<p style=\"margin-left:30px\">BackUp-Profile<\/p>\n<p>Profile Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, discusses one of his favorite functions:&nbsp;Get-EnumValues. Microsoft Scripting Guy, Ed Wilson, is here. Today I am working on my presentation for Atlanta TechStravaganza 2015, which will happen on August 21. It will be a really cool event, and the Scripting Wife and I are looking forward to attending it. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[69,51,144,3,4,45],"class_list":["post-71882","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-functions","tag-getting-started","tag-profiles","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, discusses one of his favorite functions:&nbsp;Get-EnumValues. Microsoft Scripting Guy, Ed Wilson, is here. Today I am working on my presentation for Atlanta TechStravaganza 2015, which will happen on August 21. It will be a really cool event, and the Scripting Wife and I are looking forward to attending it. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71882"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71882\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}