Variable scoping in PowerShell

ou can use scope modifiers or the *-Variable cmdlets.

The scope modifiers are:

    • global used to access/modify at the outermost scope (eg. the interactive shell)
    • script used on access/modify at the scope of the running script (.ps1 file). If not running a script then operates as global.

 

(For the -Scope parameter of the *-Variable cmdlets see the help.)

Eg. in your second example, to directly modify the global $array:

& {
  $global:array +="s"
  Write-Host $array
}

For more details see the help topic about_scopes.

How does the RemoteSigned execution policy work?

http://blogs.msdn.com/b/powershell/archive/2007/03/07/how-does-the-remotesigned-execution-policy-work.aspx

You might have wondered how the “RemoteSigned” execution policy protects us from running unsigned PowerShell scripts downloaded from the internet. We use the URL Security Zones API related to “Attachment Execution Service” (AES) introduced in Windows XP SP2 and Windows Server 2003 SP1. Internet Explorer and Outlook Express are among the applications that participate in the AES system, while FireFox, Microsoft Office Outlook and Live Messenger do not follow AES.

Continue reading “How does the RemoteSigned execution policy work?”

The object-orientedness of PowerShell and what you see may not be what you get

PS ewu\My Documents\GitHub\PowerShell> get-aduser ewu -Properties LastLogonDate | Select LastLogonDate

LastLogonDate
————-
1/1/2016 6:22:01 PM

PS ewu\My Documents\GitHub\PowerShell> get-aduser ewu -Properties LastLogonDate | Select LastLogonDate | gm

TypeName: Selected.Microsoft.ActiveDirectory.Management.ADUser

======

PS ewu\My Documents\GitHub\PowerShell> (get-aduser ewu -Properties LastLogonDate | Select LastLogonDate).LastLogonDate

Friday, January 01, 2016 6:22:01 PM

PS ewu\My Documents\GitHub\PowerShell> (get-aduser ewu -Properties LastLogonDate | Select LastLogonDate).LastLogonDate | gm

TypeName: System.DateTime

=========

PS ewu> (get-aduser ewu -Properties LastLogonDate | Select LastLogonDate).LastLogonDate

Friday, January 01, 2016 6:22:01 PM

PS ewu> ((get-aduser ewu -Properties LastLogonDate | Select LastLogonDate).LastLogonDate | Out-String) -like “*Jan*2016*”
True

PS ewu> (get-aduser ewu -Properties LastLogonDate | Select LastLogonDate | out-string)

LastLogonDate
————-
1/1/2016 6:22:01 PM

PS ewu> (get-aduser ewu -Properties LastLogonDate | Select LastLogonDate | out-string) -like “*1*2016”
False