Setting Time Zone

While you need administrative privileges to adjust time and date on your computer, each user can change the time zone, i.e. when you travel. PowerShell 5 comes with a very simple family of cmdlets to manage time zones. First, check out your current setting:

 
PS> Get-TimeZone


Id                         : W. Europe Standard Time
DisplayName                : (UTC+01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien
StandardName               : Mitteleuropäische Zeit
DaylightName               : Mitteleuropäische Sommerzeit
BaseUtcOffset              : 01:00:00
SupportsDaylightSavingTime : True
 

Next, try and change the time zone. The line below opens a window with the names of all available time zones:

 
PS> Get-TimeZone -ListAvailable | Out-GridView
 

Once you know the official ID of the time zone you want to set, use Set-TimeZone:.

 
PS> Set-TimeZone -Id 'Chatham Islands Standard Time' 

PS> Get-Date

Samstag, 27. Mai 2017 18:32:53



PS> Set-TimeZone -Id 'W. Europe Standard Time'

PS> Get-Date

Samstag, 27. Mai 2017 07:48:02 
 

Twitter This Tip! ReTweet this Tip!

Finding All Variables in a Script

In the previous tip we illustrated how you can analyze the content of a script block and search for variables or commands. The same is possible for text-based scripts. The script below will analyze itself and dump variables and commands:

$filepath = $PSCommandPath
$tokens = $errors = $null

$ast = [System.Management.Automation.Language.Parser]::ParseFile($filepath, [ref]$tokens, [ref]$errors )

# find variables
$ast.FindAll( { $true }, $true) | 
  Where-Object { $_.GetType().Name -eq 'VariableExpressionAst' } |
  Select-Object -Property VariablePath -ExpandProperty Extent |
  Select-Object -Property * -ExcludeProperty *ScriptPosition |
  Out-GridView -Title 'Variables'


# find commands
$ast.FindAll( { $true }, $true)  | 
  Where-Object { $_.GetType().Name -eq 'CommandAst' } |
  Select-Object -ExpandProperty Extent  |
  Select-Object -Property * -ExcludeProperty *ScriptPosition |
  Out-GridView -Title 'Commands'

Just make sure the script is saved to disk, or specify a different path to a valid script in $filepath.

Twitter This Tip! ReTweet this Tip!

Finding All Variables in a Script Block

To analyze the content of a script block, you can easily examine the AST, and, for example, create a list of all variables in the code:

$code = {

    $a = "Test"
    $b = 12
    Get-Service
    Get-Process
    $berta = 100

}


$code.Ast.FindAll( { $true }, $true) | 
  Where-Object { $_.GetType().Name -eq 'VariableExpressionAst' } |
  Select-Object -Property VariablePath -ExpandProperty Extent |
  Out-GridView

If you’d like to see all commands instead, try this:

$code = {

    $a = "Test"
    $b = 12
    Get-Service
    Get-Process
    $berta = 100

}


$code.Ast.FindAll( { $true }, $true)  | 
  Where-Object { $_.GetType().Name -eq 'CommandAst' } |
  Select-Object -ExpandProperty Extent  |
  Select-Object -Property * -ExcludeProperty *ScriptPosition |
  Out-GridView

This can be useful to create auto-generated documentation for script blocks.

Twitter This Tip! ReTweet this Tip!