PowerShell - Variable

Windows Powershell Menu

Structure

A variable in Powershell is an object. It has then member (of properties)

Management

Member

  • See the member of a variable
$loc = $pwd
$loc | Get-Member -MemberType Property
TypeName: System.Management.Automation.PathInfo

Name         MemberType Definition
----         ---------- ----------
Drive        Property   System.Management.Automation.PSDriveInfo Drive {get;}
Path         Property   string Path {get;}
Provider     Property   System.Management.Automation.ProviderInfo Provider {get;}
ProviderPath Property   string ProviderPath {get;}

Function

  • List the functions to manipulate a variable
Get-Command -Noun Variable | Format-Table -Property Name,Definition -AutoSize -Wrap

Is Set

A variable full scope name is expressed with the following URI

Variable:\foo
variable:global:foo

and can be tested with the Test_path cmdlet

if (!(Test-Path Variable:\ORACLE_DB_PWD)){
  Write-Error 'The variable $ORACLE_DB_PWD is not set and is mandatory for the installation of the oracle database'
  exit(1)
}

Is Empty String

  • From an argument, if not set, you get an empty string.
$command = $args[1]

if (!($command)) {
	Write-Error "A command must be given because I got an empty string"
	Print-Usage
	exit(1)
}

Is Not Null

null is returned when nothing is found

$VAR -ne $null
# or is null
$VAR -eq $null

Scope

# global variables
$global:lastpercentage = -1

The scope of variable defined in other script are dependent of the type of calling. See PowerShell - Script

Print

Write-host "DSN Attribute of: $($_.Name)"

Documentation / Reference







Share this page:
Follow us:
Task Runner