{"id":2198,"date":"2016-04-11T00:00:00","date_gmt":"2016-04-11T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/questions-about-powershell-basics-that-you-were-too-shy-to-ask\/"},"modified":"2017-09-13T17:45:32","modified_gmt":"2017-09-13T17:45:32","slug":"questions-about-powershell-basics-that-you-were-too-shy-to-ask","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/questions-about-powershell-basics-that-you-were-too-shy-to-ask\/","title":{"rendered":"Questions About PowerShell Basics That You Were Too Shy To Ask"},"content":{"rendered":"<div class=\"article-content\">\n<ol>\n<li><a href=\"#first\">Does PowerShell Support Object-oriented Programming (OOP)?<\/a><\/li>\n<li><a href=\"#second\">Is PowerShell&#8217;s Execution Policy a security layer?<\/a><\/li>\n<li><a href=\"#third\">Can I bypass the Execution Policy Setting?<\/a><\/li>\n<li><a href=\"#fourth\">Can I use cmd (dos batch file) commands in PowerShell?<\/a><\/li>\n<li><a href=\"#fifth\">I saw, in a script, a variable defined as $script:myvariable. What does $script mean?<\/a><\/li>\n<li><a href=\"#sixth\">What are the symbols $_ and $_. ?<\/a><\/li>\n<li><a href=\"#seventh\">If Everything in PowerShell is an object, how can I know all the properties, methods, whatever&#8230; all these items from an object?<\/a><\/li>\n<li><a href=\"#eighth\">What is Variable Interpolation?<\/a><\/li>\n<li><a href=\"#ninth\">What is the {0} -f that I saw in some string codes?<\/a><\/li>\n<li><a href=\"#tenth\">What are Scriptblocks?<\/a><\/li>\n<li><a href=\"#eleventh\">How can I change a name of a property or add a new one in a output of an cmdlet?<\/a><\/li>\n<li><a href=\"#twelveth\">What is dot-sourcing?<\/a><\/li>\n<li><a href=\"#thirteenth\">In PowerShell, I&#8217;m told we can navigate within the registry as if it were a file system. How can this be possible?<\/a><\/li>\n<li><a href=\"#fourteenth\">I was writing a function and, when checking other codes, I saw a CMDLETBINDING word. What is this good for?<\/a><\/li>\n<\/ol>\n<h2 id=\"first\">Does PowerShell Support Object-oriented Programming (OOP)?<\/h2>\n<p class=\"start\">Yes, PowerShell is built on the top of the .NET framework, so it is safe to say that it supports Object-oriented Programming. A good definition of PowerShell is:<\/p>\n<p><em>&#8220;Windows PowerShell is the new standard Windows command-line shell. Windows PowerShell includes a runtime engine, data providers, core commands (known as cmdlets), a new scripting language, and an interactive prompt. Windows <strong>PowerShell is completely object-oriented <\/strong>and processes cmdlets, script files (.ps1), and executable files.&#8221; <\/em><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/cc303698.aspx\">Developing with Windows PowerShell<\/a><\/p>\n<p>Before PowerShell 5.0, this was not entirely true. It would be more accurate to use a phrase that my fellow MVP Keith Hill used: &#8220;PowerShell is more of an OOP consumer language.&#8221;<\/p>\n<p>One of the most exciting and powerful features that were introduced into PowerShell 5.0 was the support for building .NET classes. In the previous versions, the simplest way to create custom objects in PowerShell was by using the <code>[PSCUSTOMOBJECT]<\/code> or <code>new-object<\/code> and build a .NET class in C# and, with the <code>add-type<\/code> cmdlet, import it to PowerShell.<\/p>\n<p>For instance:<\/p>\n<h3>Before PowerShell 5.0:<\/h3>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">Function New-Sith {\r\n     param(\r\n            [Parameter(Mandatory=$true)]\r\n            [string]$Name,\r\n            [Parameter(Mandatory=$true)]\r\n            [ValidateSet('beginner', 'Good','VeryGood', 'Master')]\r\n            [string]$KnowldgeOfTheForce\r\n    )\r\n    New-Object psobject -property @{\r\n        Name = $Name\r\n        KnowldgeOfTheForce = $KnowldgeOfTheForce\r\n        Color = $color\r\n        HeSheIsBad = $HeSheIsBad\r\n    }\r\n\r\n}\r\n\r\nFunction Invoke-ImperialMarch {\r\n\r\n    param(\r\n        [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]\r\n        [String]$Name\r\n    )\r\n    process {\r\n        \"{0} says : I find your lack of PowerShell disturbing...\" -f $Name\r\n        #original imperial march code from Jeff Wouters\r\n        #http:\/\/jeffwouters.nl\/index.php\/2012\/03\/get-your-geek-on-with-powershell-and-some-music\/\r\n        [console]::beep(440,500) \r\n        [console]::beep(440,500) \r\n        [console]::beep(440,500) \r\n        [console]::beep(349,350) \r\n        [console]::beep(523,150) \r\n        [console]::beep(440,500) \r\n        [console]::beep(349,350) \r\n        [console]::beep(523,150) \r\n        [console]::beep(440,1000) \r\n        [console]::beep(659,500) \r\n        [console]::beep(659,500) \r\n        [console]::beep(659,500) \r\n        [console]::beep(698,350) \r\n        [console]::beep(523,150) \r\n        [console]::beep(415,500) \r\n        [console]::beep(349,350) \r\n        [console]::beep(523,150) \r\n        [console]::beep(440,1000)\r\n    }\r\n\r\n}\r\n\r\nNew-Sith -Name 'Darth Laertus' -KnowldgeOfTheForce Master |\r\nInvoke-ImperialMarch \r\n\r\nPowerShell 5.0\r\n\r\nClass Sith {\r\n    [String] $Name;\r\n    [String] $KnowldgeOfTheForce;\r\n\r\n    Sith([String] $NewName, [String] $NewKnowldgeOfTheForce) {\r\n\r\n        $this.Name = $NewName;\r\n     \r\n        $this.KnowldgeOfTheForce = $NewKnowldgeOfTheForce;\r\n        \r\n    }\r\n\r\n}\r\n\r\n$NewSith = ([Sith]::new('Darth Laertus','Master'))\r\n$NewSith |\r\nInvoke-ImperialMarch \r\n<\/pre>\n<p>You can implement a constructor, methods, whatever else you want; the entire class definition. Indeed, it was good news for PowerShell developers, but the real unspoken question when developers ask if a language is OOP supported is &#8220;Can I use PowerShell to write a full ERP system?<\/p>\n<p>Actually, you can, but you would not want to. PowerShell is intended to simplify and automate administrative tasks on Windows-based systems. Of course, it supports, by legacy, programming languages concepts, but it is not its basic purpose. You can write, for instance, a full CRM system in C++ or even in PowerShell, but in my humble opinion, it is equivalent to using a Ferrari in a farm to carry the milk churns.<\/p>\n<p>You can read more about the implementation of classes in PowerShell 5.0 here:<\/p>\n<ol>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/08\/31\/introduction-to-powershell-5-classes\/\">Hey, Scripting Guy! Blog- Introduction to PowerShell 5 Classes<\/a><\/li>\n<li><a href=\"http:\/\/trevorsullivan.net\/2014\/10\/25\/implementing-a-net-class-in-powershell-v5\/\">Implementing a .NET Class in PowerShell v5<\/a><\/li>\n<\/ol>\n<h2 id=\"second\">Is PowerShell&#8217;s Execution Policy a security layer?<\/h2>\n<p>No. The execution policy in PowerShell is part of the security strategy but merely prevents potentially malicious scripts being executed by accident and, with the &#8216; <em>allsigned<\/em> <em>&#8216;<\/em> policy ensures that scripts cannot be altered without your knowledge. By default, this setting is set to &#8216; <em>Restricted<\/em> <em>&#8216;<\/em>, meaning that no PowerShell script file can be run but it will execute PowerShell code within the PowerShell console. If you try to run a .ps1 script from file with execution policy set to restricted, you will face the message:<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">File C:\\Laerte\\Test.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see \r\nabout_Execution_Policies at http:\/\/go.microsoft.com\/fwlink\/?LinkID=135170.\r\n    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException\r\n    + FullyQualifiedErrorId : UnauthorizedAccess \r\n<\/pre>\n<p>&#8230; but if you copy code from the <code>.ps1<\/code> file and paste it into the PowerShell console it will be executed.<\/p>\n<h2 id=\"third\">Can I bypass the Execution Policy Setting?<\/h2>\n<p>Yes. You can execute a PowerShell script file by calling it from a <code>.bat <\/code>shell-script <code><\/code>file: This will bypass the execution policy in the system. Actually there are several ways to bypass the execution policy, but in this example, it is achieved merely by calling PowerShell.exe using the &#8216; <em>bypass<\/em>&#8216; option in the -ExecutionPolicy parameter.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-2016-03-30_8-56-28-bb43228c-a553-4684-a4ea-8f62aab3f89b.png\" alt=\"2399-2016-03-30_8-56-28-bb43228c-a553-46\" \/><\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">PowerShell.exe -ExecutionPolicy Bypass -File c:\\laerte\\test.ps1 <\/pre>\n<p>For more information about execution policies:<\/p>\n<ol>\n<li><a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/hh849812.aspx\">TECHNET &#8211; Set-ExecutionPolicy<\/a><\/li>\n<\/ol>\n<h2 id=\"fourth\">Can I use cmd (dos batch file) commands in PowerShell?<\/h2>\n<p>Yes, you can. PowerShell can launch external programs in almost the same way as <code>cmd<\/code> and it accepts all the <code>cmd<\/code> commands as legacy.<\/p>\n<p class=\"illustration\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-cmdnew.png\" alt=\"2399-cmdnew.png\" width=\"620\" height=\"443\" \/><\/p>\n<h2 id=\"fifth\">I saw, in a script, a variable defined as $script:myvariable. What does $script mean?<\/h2>\n<p>Variables, aliases, functions and drives in PowerShell all work within &#8216;scopes&#8217;. This simply means that they are visible &#8211; or available &#8211; and changeable (mutable) only in the part of a script that you intend. This prevents values being changed unintentionally. In PowerShell, we can define four types of scope: <em>global<\/em> <em>, <\/em> <em>local, private<\/em> and <em> script<\/em><\/p>\n<p>When you include an item (or items) within a scope, by running a script or function, creating a new session or starting a new instance of PowerShell, it will be visible only in the scope within which it was created and in any child scope. This rule does not apply to private scope. In the same way, it can only be accessed or changed in its original scope. If you use the same name for another item in a different scope, the original variable will be hidden under the new item, but will not be overridden or changed.<\/p>\n<p>For instance, if you create a variable inside a function, you cannot access (or change) its value in the script level where the function was called.<\/p>\n<h3>Global Scope<\/h3>\n<p>The global scope is the scope created when PowerShell starts. Any Item that is created when PowerShell starts or created with <em>$global<\/em> will be visible in the entire session and in all other scopes, as long it is alive. It is only when your code enters a nested prompt, script, block or function that a child scope is created. You can force a variable to be in global scope by using the $global: prefix to the variable.<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing \">$Global:IamGlobalBaby = \"Now you see me\"\r\n\r\nfunction InsideMeHaveOtherScope {\r\n\r\n    write-host $Global:IamGlobalBaby \r\n    $Global:IamGlobalBaby = \"I was changed in another scope because I am Global\"\r\n\r\n}\r\n\r\nInsideMeHaveOtherScope\r\n\r\n$Global:IamGlobalBaby  \r\n<\/pre>\n<h3>Local Scope<\/h3>\n<p>The &#8216;local scope&#8217; is the default scope when you create a variable with no scope defined. Any item that is created within a function is available only within the script of function.<\/p>\n<h3>Script Scope<\/h3>\n<p>It is available from all scopes within the script, or in any script called within the parent script. While the script is running, you can think as it is the same a Global scope<\/p>\n<p>For instance:<\/p>\n<pre class=\"theme:powershell-ise lang:ps listing\">$IamLocal = \"Now you See me as local\"\r\nFunction InsideMeHaveOtherScope {\r\n\r\n    $IamLocal = \"Now you See me as script scope\"\r\n\r\n    write-host $local:IamLocal\r\n    write-host $Script:IamLocal\r\n\r\n}\r\nInsideMeHaveOtherScope \r\nWrite-Host '--------------------------'\r\nwrite-host $local:IamLocal\r\nwrite-host $Script:IamLocal \r\n<\/pre>\n<p>In the next example below, I show how the <code>script <\/code>and <code>local<\/code> prefixes work to change values. In this script, I first created a variable called <code>IamScript<\/code> <code><\/code>in the <code>Script <\/code>scope. Inside the function, I could access the value but I then tried to change the value not specifying its scope, so it created a new variable, locally only to the function with the same name.<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$Script:IamScript = \"I am script scope\"\r\n\r\nFunction InsideMeHaveOtherScope {\r\n\r\n\r\n    Write-host $Script:IamScript\r\n    $IamScript = \"Changed the value, but in another scope. It created a new variable\"\r\n    Write-host $Script:IamScript\r\n    Write-host $IamScript\r\n}\r\n\r\nInsideMeHaveOtherScope \r\n<\/pre>\n<p>If I want to change the value of the variable, I need to specify the scope:<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$Script:IamScript = \"I am script scope\"\r\n\r\nFunction InsideMeHaveOtherScope {\r\n\r\n\r\n    Write-host $Script:IamScript\r\n    $Script:IamScript = \"Changed the value in the script scope, inside the function\"\r\n    Write-host $Script:IamScript\r\n\r\n}\r\n\r\nInsideMeHaveOtherScope\r\n\r\nWrite-host $Script:IamScript \r\n<\/pre>\n<h2 id=\"sixth\">What are the symbols $_ and $_. ?<\/h2>\n<p>The $_ variable-name is placeholder. Actually every script language uses placeholders and variables to store data. In this case, we could say that it where the data that comes out from a pipeline is held. In PowerShell everything is an object, and when you refer to a <code>$<\/code> <code>_<\/code> , you are using the entire object that is coming from the pipeline: Of course, you can also also choose a property, method or any item from this object using <em>$_.Property<\/em> syntax <em>.<\/em><\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">Get-service | \r\nWhere-Object {\r\n    $_.Name -like '*SQL*'\r\n} \r\n<\/pre>\n<p>In PowerShell 3.0, an alias to <code>$_ <\/code>was introduced, called <code>$<\/code> <code>PSItem<\/code><\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">Get-service | \r\nWhere-Object {\r\n    $PSItem.Name -like '*SQL*'\r\n} \r\n<\/pre>\n<p><code>$_<\/code> actually works like <code>$this<\/code> except that it means &#8216;this object&#8217;. Just as you can use it in a pipeline, you can use it in a Switch statement or any other processing structure.<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$MyInput='LaerteJunior','TonyJunior','SimpleTalkEditor'\r\n\r\nswitch -wildcard($MyInput)\r\n{&lt;code data-mce-bogus=\"1\"&gt;&lt;\/code&gt;\r\n    \"L*\" {\"$_ begins with the letter 'L'\"}\r\n    \"*Junior*\" {\"$_'s Dad was called $($_ -ireplace 'Junior','')\"}\r\n\r\n} \r\n<\/pre>\n<p>Which would give&#8230;<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">LaerteJunior begins with 'L'\r\nLaerteJunior's Dad was called Laerte\r\nTonyJunior's Dad was called Tony \r\n<\/pre>\n<h2 id=\"seventh\">If Everything in PowerShell is an object, how can I know all the properties, methods, whatever&#8230; all these items from an object?<\/h2>\n<p>Use the cmdlet Get-Member<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">Get-Process | \r\nGet-Member \r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-2016-04-04_6-30-32-b3e011b3-ffe0-438a-a63a-08189ad166bf.png\" alt=\"2399-2016-04-04_6-30-32-b3e011b3-ffe0-43\" \/><\/p>\n<h2 id=\"eighth\">What is Variable Interpolation?<\/h2>\n<p>When you add a variable to a double-quoted string, PowerShell replaces the variable name by its value. This feature is called variable interpolation.<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$MasterSith = \"Darth Vader\" \r\n\"The most powerfull Sith ever is $MasterSith\" \r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-2016-04-04_6-57-59-7b1feb8e-1ae1-4b62-a2a6-841a2e2a03a1.png\" alt=\"2399-2016-04-04_6-57-59-7b1feb8e-1ae1-4b\" \/><\/p>\n<p>You can hit a problem with variable interpolation if you include an object property (or even a result of a method) in the string. The standard notation of (dot property ) or ( <code>.<\/code> <code>myproperty<\/code>) does not work as expected . For this problem, PowerShell has a subexpression operator $(). This operator first evaluates and run what is inside the brackets to resolve it into a simple value before passing it to the string for interpolation.<\/p>\n<p>So here is a PowerShell script that will hit problems<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$SQLBrowser = Get-Service -Name \"SQL Server Browser\"\r\n\"SQL Server Browser status is $SQLBrowser.status\" \r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-2016-04-04_7-10-00-96d0355c-b621-43c0-a0fd-42171cbbdcc5.png\" alt=\"2399-2016-04-04_7-10-00-96d0355c-b621-43\" \/><\/p>\n<p>So you need to use the <code>$()<\/code> syntax. In this case, PowerShell will evaluate the <em>$<\/em> <em>SQLBrowser.status<\/em> first, and then will interpolate the rest of the string.<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$SQLBrowser = Get-Service -Name \"SQL Server Browser\"\r\n\"SQL Server Browser status is $($SQLBrowser.status)\" \r\n<\/pre>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">PS C:\\Users\\Administrator&gt; $SQLBrowser = Get-Service -Name \"SQL Server Browser\"\r\n\"SQL Server Browser status is $($SQLBrowser.status)\"\r\nSQL Server Browser status is Stopped\r\n\r\nPS C:\\Users\\Administrator&gt;  \r\n<\/pre>\n<p>Another very important point is that the variable interpolation only happens in double-quoted strings so if you will don&#8217;t need interpolation, then use a single-quoted string instead.<\/p>\n<h2 id=\"ninth\">What is the {0} -f that I saw in some string codes?<\/h2>\n<p>This is string formatting using the PowerShell format operator. In PowerShell, you do not need to use &#8220;+&#8221; operator to concatenate strings. Actually is more in-tune with PowerShell syntax if you use interpolation or the <code>-f<\/code> (from format)<\/p>\n<p>It is simple. You add the placeholder {N} and then type a command-separated list of values in order to be replaced. {0} represents the first value in the command-separated list, {1} the second and so on.<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">\"Hello I am {0}, I am {1} and so on\" -f \"the first value\",\"the second one\" <\/pre>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">PS C:\\Users\\Administrator&gt; \"Hello I am {0}, I am {1} and so on\" -f \"the first value\",\"the second one\"\r\nHello I am the first value, I am the second one and so on\r\n<\/pre>\n<p>You can, of course, do this more simply with variable interpolation, but the Format operator is the one to use if you need to format numbers, currencies, scientific notations, and dates. You can specify precision, separators and any or all fields within a date. You can also use it to align text.<\/p>\n<h2 id=\"tenth\">What are Scriptblocks?<\/h2>\n<p>Scriptblocks are one of most Powerful concept of PowerShell. By definition, is a collection of statements or expressions between &#8220;{&#8221; and &#8220;}&#8221; that can be used as a single unit. It can be stored in a variable, can be passed to function and can be executed remotely by the operator &#8220;&amp;&#8221;<\/p>\n<p>Syntax :<\/p>\n<p>{&lt;statement list&gt;}<\/p>\n<p>Like functions, a scriptblock can include parameters :<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">{\r\n            param ([type]$parameter1 [,[type]$parameter2])\r\n            \r\n }\r\n<\/pre>\n<p>And script blocks can include the DynamicParam, Begin, Process, and End keywords.<\/p>\n<h3>The Basics<\/h3>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$MyScriptBlock = {\"Hey, I am alive\"}\r\n&amp; $MyScriptBlock  \r\n<\/pre>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">PS C:\\Users\\Administrator&gt; \r\n$MyScriptBlock = {\"Hey, I am alive\"}\r\n&amp; $MyScriptBlock \r\nHey, I am alive\r\n\r\nPS C:\\Users\\Administrator&gt;  \r\n<\/pre>\n<h3>Adding Parameters<\/h3>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$MyScriptBlock = { param ($param1) \"Hey, I am alive and I love $($param1)\"}\r\n&amp; $MyScriptBlock -param1 \"Star Wars\" \r\n<\/pre>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">PS C:\\Users\\Administrator&gt; \r\n$MyScriptBlock = { param ($param1) \"Hey, I am alive and I love $($param1)\"}\r\n&amp; $MyScriptBlock -param1 \"Star Wars\"\r\nHey, I am alive and I love Star Wars \r\n<\/pre>\n<h3>Running Remotely<\/h3>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing\">$MyScriptBlock = { \"Hey, I am alive and I love\"}\r\n\r\nInvoke-Command -ComputerName MyComputer -ScriptBlock $MyScriptBlock  \r\n<\/pre>\n<h2 id=\"eleventh\">How can I change a name of a property or add a new one in a output of an cmdlet ?<\/h2>\n<p>You may use custom tables.<\/p>\n<p>Custom tables are a especial type of formatting output that uses the constructor @{} with the elements <em>Expression <\/em>and <em>Name <\/em>inside it , separated by semi-colon, to customize the output :<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true listing \">@{Expression = {};Name = &lt;Name&gt;} <\/pre>\n<p><em>Expression<\/em> is the data you want to display. Note that it is a scriptblock, so you can only display a property or write an entire script inside it.<\/p>\n<p><em>Name<\/em> is the header to appear in the top of the column. But how it can be used? Let&#8217;s check some examples:<\/p>\n<p>If you run a Get-Process you will have the output :<\/p>\n<pre class=\"theme:powershell-ise lang:ps listing\">Get-Process  <\/pre>\n<pre class=\"theme:powershell-output lang:ps listing\">PS C:\\Users\\Administrator&gt; Get-Process \r\n\r\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                                                                                                            \r\n-------  ------    -----      ----- -----   ------     --  -- -----------                                                                                                            \r\n    282      16     8336      19360 ...99     0.70   7404   2 ApplicationFrameHost                                                                                                   \r\n    181      11     7300      10852 ...97    89.06   9532   0 audiodg                                                                                                                \r\n    676     820    30096      47972   282 ...63.73   2644   2 BitTorrent                                                                                                             \r\n    158      16     5600      14428   111   827.42  10804   2 calendar                                                                                                               \r\n    214      17     2924       9492    80     3.03  11160   0 CalendarServ                                                                                                           \r\n    381      35   114600     115188   463 2,712.31   2500   2 chrome                                                                                                                 \r\n    240      22    38712      23528   230    12.69   4344   2 chrome                                                                                                                 \r\n    430      44   185204     185288   591 3,265.36   4564   2 chrome                                                                                                                 \r\n    276      31    91840      44128   358    32.25   6344   2 chrome                                                                                                                 \r\n    378      53   233440     218840   652 5,607.91   7840   2 chrome                                                                                                                 \r\n    348      43   206756     184964   581 3,644.78   8068   2 chrome   \r\n<\/pre>\n<p>Well, the header names are a bit hard to understand. I am a Brazilian and want to just show the <code>Handles<\/code> and <code><\/code> <code>ProcessName<\/code> Columns as they are, but I would like the &#8216; <em>ProcessName<\/em> <em>&#8216; <\/em>title translated to Portuguese :<\/p>\n<pre class=\"theme:powershell-ise lang:ps listing\">Get-Process |\r\nSelect handles,\r\n       @{Expression = {$_.ProcessName};Name = \"Nome Do Processo\"}  \r\n<\/pre>\n<pre class=\"theme:powershell-output lang:ps listing\">PS C:\\Users\\Administrator&gt; Get-Process |\r\nSelect handles,\r\n       @{Expression = {$_.ProcessName};Name = \"Nome Do Processo\"} \r\n\r\nHandles Nome Do Processo    \r\n------- ----------------    \r\n    282 ApplicationFrameHost\r\n    181 audiodg             \r\n    686 BitTorrent          \r\n    158 calendar            \r\n    214 CalendarServ        \r\n    381 chrome     \r\n<\/pre>\n<p>Now I want to add the property <code>ComputerName<\/code> :<\/p>\n<pre class=\"theme:powershell-ise lang:ps listing\">Select handles,\r\n       @{Expression = {$_.ProcessName};Name = \"Nome Do Processo\"} ,\r\n       @{Expression = {\"MyComputerName\"};Name = \"Computer Name\"}  \r\n<\/pre>\n<pre class=\"theme:powershell-output lang:ps listing\">PS C:\\Users\\Administrator&gt; Get-Process |\r\nSelect handles,\r\n       @{Expression = {$_.ProcessName};Name = \"Nome Do Processo\"} ,\r\n       @{Expression = {\"MyComputerName\"};Name = \"Computer Name\"} \r\n\r\n\r\n\r\nHandles Nome Do Processo     Computer Name \r\n------- ----------------     ------------- \r\n    282 ApplicationFrameHost MyComputerName\r\n    181 audiodg              MyComputerName\r\n    671 BitTorrent           MyComputerName\r\n    158 calendar             MyComputerName \r\n<\/pre>\n<p>As I said before, the Expression is a scriptblock so I can call a function inside it:<\/p>\n<pre class=\"theme:powershell-ise lang:ps listing\">function Show-Something { \r\n    param ($param)\r\n    Write-Output \"I am outputing something\"\r\n} \r\n\r\nGet-Process |\r\nSelect handles,\r\n       @{Expression = {$_.ProcessName};Name = \"Nome Do Processo\"} ,\r\n       @{Expression = {\"MyComputerName\"};Name = \"Computer Name\"} ,\r\n       @{Expression = {Show-Something -param 'Foo'};Name = \"Called a Function\"} \r\n<\/pre>\n<pre class=\"theme:powershell-ise lang:ps listing\">Get-Process |\r\nSelect handles,\r\n       @{Expression = {$_.ProcessName};Name = \"Nome Do Processo\"} ,\r\n       @{Expression = {\"MyComputerName\"};Name = \"Computer Name\"} ,\r\n       @{Expression = {Show-Something -param 'Foo'};Name = \"Called a Function\"} \r\n\r\n\r\n\r\nHandles Nome Do Processo     Computer Name  Called a Function       \r\n------- ----------------     -------------  -----------------       \r\n    282 ApplicationFrameHost MyComputerName I am outputing something\r\n    181 audiodg              MyComputerName I am outputing something\r\n    666 BitTorrent           MyComputerName I am outputing something\r\n    158 calendar             MyComputerName I am outputing something\r\n    216 CalendarServ         MyComputerName I am outputing something\r\n    380 chrome               MyComputerName I am outputing something\r\n    240 chrome               MyComputerName I am outputing something\r\n    430 chrome               MyComputerName I am outputing something \r\n<\/pre>\n<p>Note : the <code>Expression<\/code> and <code>Name<\/code> elements can be shorted to E and N<\/p>\n<pre class=\"theme:powershell-ise lang:ps listing\">Get-Process |\r\nSelect handles,\r\n       @{E = {$_.ProcessName};N = \"Nome Do Processo\"} ,\r\n       @{E = {\"MyComputerName\"};N = \"Computer Name\"} ,\r\n       @{E = {Show-Something -param 'Foo'};N = \"Called a Function\"}  \r\n<\/pre>\n<h2 id=\"twelveth\">What is dot-sourcing?<\/h2>\n<p>In simple words, it is the way that you can make available in your current scope such items as variables or functions from another script.<\/p>\n<p>As we saw before, scripts or functions have their own scope. So if I start a PowerShell session and want to load items in the current scope and keep them visible in the session, the way you can do it is by dot-sourcing this script.<\/p>\n<p>Let&#8217;s create a function called <code>foo<\/code> that just displays a message in the screen:<\/p>\n<pre class=\"theme:powershell-ise lang:ps listing\">Function Get-Foo {\r\n\r\n    Write-Host \"Hey I am alive\"\r\n} \r\n<\/pre>\n<p>Then in the PowerShell host call this function in a standard PowerShell notation (.\\NameofTheScript):<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-1-52bf74bd-0ceb-44e3-b6fe-171c3ebb13f4.png\" alt=\"2399-1-52bf74bd-0ceb-44e3-b6fe-171c3ebb1\" \/><\/p>\n<p>If I try to load the function again as a native cmdlet (just the name of the function) I will get an error because it was visible only when was called and destroyed after that.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-1-407d6f0d-34b0-45ac-a3b4-a69d560b1764.png\" alt=\"2399-1-407d6f0d-34b0-45ac-a3b4-a69d560b1\" \/><\/p>\n<p>But if I dot source this function and then try to call as a native cmdlet, it will work. Everything inside that .ps1 file now is available in the current scope.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-1-ea2ed633-ebd0-4e1e-b8e2-d77687434048.png\" alt=\"2399-1-ea2ed633-ebd0-4e1e-b8e2-d77687434\" \/><\/p>\n<h2 id=\"thirteenth\">In PowerShell, I&#8217;m told we can navigate within the registry as if it were a file system. How can this be possible?<\/h2>\n<p>Amazing, huh?<\/p>\n<p>Yes, in PowerShell we have providers. In simple words, they are little .NET programs built in in PowerShell that allow you work with data as a file system.<\/p>\n<p>We can check all the PowerShell providers by using the <em>Get-<\/em> <em>PSProvider<\/em> cmdlet :<\/p>\n<pre class=\"listing\">Get-PSProvider<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/2399-42091764-fedd-4ebe-9788-d09f3c9afab2.png\" alt=\"2399-42091764-fedd-4ebe-9788-d09f3c9afab\" \/><\/p>\n<p>In the first column we have the name of the provider, followed by Capabilities &#8211; what the provider supports &#8211; and then the driver to access this provider.<\/p>\n<p>As I said, you will work with these as file system; so to access the registry provider, just use the <em>CD Drive:<\/em> notation<\/p>\n<pre class=\"theme:powershell-output lang:ps listing\">PS C:\\Users\\Administrator&gt; cd HKLM:\r\n\r\nPS HKLM:\\&gt; dir\r\n\r\n\r\n    Hive: HKEY_LOCAL_MACHINE\r\n\r\n\r\nName                           Property                                                                                                                                              \r\n----                           --------                                                                                                                                              \r\nBCD00000000                                                                                                                                                                          \r\nHARDWARE                                                                                                                                                                             \r\nSAM                                                                                                                                                                                  \r\nSECURITY                                                                                                                                                                             \r\nSOFTWARE                                                                                                                                                                             \r\nSYSTEM                                                                                                                                                                               \r\n\r\n\r\n\r\nPS HKLM:\\&gt;  \r\n<\/pre>\n<p>Then it is just a matter of changing the &#8220;subfolders &#8220;to keep navigating within it. ( <code>CD \\ <\/code> <code>Software<\/code> ..etc)<\/p>\n<p>It is not only the registry, as you can see. You can do the same for variables, certificates, functions, Environment variables, aliases and so on \u00a0..<\/p>\n<h2 id=\"fourteenth\">I was writing a function and, when checking other codes, I saw a CMDLETBINDING word. What is this good for?<\/h2>\n<p>It&#8217;s simple. You are saying to your function &#8220;Hey beautiful, please from now on work as a compiled cmdlet&#8221;.<\/p>\n<p>When you define the <code><\/code> <code>cmdletbinding<\/code> attribute in a function, PowerShell binds its parameters the same way as it does the C# compiled cmdlets. This means this function is now an advanced function and it has access to all the features of the compiled cmdlets, as common parameters.<\/p>\n<p>Functions, Advanced Functions are a huge topic. Please refer to my previous article: <a href=\"https:\/\/www.simple-talk.com\/sql\/sql-tools\/the-posh-dba-grown-up-powershell-functions\/\">The PoSh DBA: Grown-Up PowerShell Functions<\/a>.<\/p>\n<p>That&#8217;s it guys. I\u00b4ve tried to address some of the questions that I had when I first started out with PowerShell, but if your questions are not in this list, please don&#8217;t be shy, email me <a href=\"mailto:laertesqldba@outlook.com\">laertesqldba@outlook.com<\/a> and I will do my best to help.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Developers who are already familiar with  application languages will be baffled by different aspects of PowerShell to the beginner to programming. Laerte recalls his initial struggles with PowerShell and answers those questions he wished he&#8217;d found quick answers to. &hellip;<\/p>\n","protected":false},"author":221715,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[35],"tags":[5771],"coauthors":[6819],"class_list":["post-2198","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-too-shy"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221715"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2198"}],"version-history":[{"count":7,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2198\/revisions"}],"predecessor-version":[{"id":72682,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2198\/revisions\/72682"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2198"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}