Difference between revisions of "Variables (PowerShell)"

Jump to navigation Jump to search
1,611 bytes added ,  14:42, 21 June 2013
Page deperciated
m (→‎Datetime: Minor rewording)
(Page deperciated)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Depreciated|category=Variables_(PowerShell)}}
  __TOC__
  __TOC__


Line 307: Line 309:
== Arrays ==
== Arrays ==
<source lang="powershell">
<source lang="powershell">
$array = @()           # Create blank array
$array = @()                 # Create blank array
$array += 34           # Add value to end of array
$array = @("one", "two", 3)  # Create an array with some values
$array += 34                 # Add value to end of array
</source>
</source>


Line 381: Line 384:
To force the creation of a .NET array from a PowerShell CmdLet, create one in a fashion such as this...
To force the creation of a .NET array from a PowerShell CmdLet, create one in a fashion such as this...
<source lang="powershell">
<source lang="powershell">
$a = New-Object System.Collections.ArrayList(,(Get-Content test.txt))
$a = New-Object System.Collections.ArrayList                                        # Empty array
$a = New-Object System.Collections.ArrayList(,(Get-Content test.txt))               # Populated with contents of test.txt
</source>
</source>


Line 391: Line 395:
</source>
</source>


== Datetime ==
== Object Types ==
=== Datetime ===
The object structure: http://msdn.microsoft.com/en-us/library/system.datetime.aspx
The object structure: http://msdn.microsoft.com/en-us/library/system.datetime.aspx


=== Improper (US) Formatting ===
==== Improper (US) Formatting ====
Be aware that if you live somewhere dates are normally formatted properly (eg not the USA), then PowerShell (or probably the underlying DateTime object type as this sort of problem seems to rear its head at unexpected moments when working on Windows) has a nasty habit of returning a string formatted with day and month swapped around when coming from a script.
Be aware that if you live somewhere dates are normally formatted properly (eg not the USA), then PowerShell (or probably the underlying DateTime object type as this sort of problem seems to rear its head at unexpected moments when working on Windows) has a nasty habit of returning a string formatted with day and month swapped around when coming from a script.


Line 401: Line 406:
If your dates are getting mixed up, it may not be your mistake, and it may be that you've fallen fowl of the problem as well.
If your dates are getting mixed up, it may not be your mistake, and it may be that you've fallen fowl of the problem as well.


=== Formatting ===
==== Formatting ====
To control how a DateTime is displayed you can pass it through <code> Get-Date </code> with the '''<code> -uFormat </code>''' option...
To control how a DateTime is displayed you can pass it through <code> Get-Date </code> with the '''<code> -uFormat </code>''' option...
<source lang="powershell">Get-Date $datetime -uFormat "%R hrs, %a %d %b %Y"</source>
<source lang="powershell">Get-Date $datetime -uFormat "%R hrs, %a %d %b %Y"</source>
Line 437: Line 442:
For the full list of formatting options see http://technet.microsoft.com/en-us/library/ee692801.aspx, and even more detail at http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
For the full list of formatting options see http://technet.microsoft.com/en-us/library/ee692801.aspx, and even more detail at http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx


=== Converters ===
==== Converters ====
<source lang="powershell">
<source lang="powershell">
function ConvertLocalToUnix([datetime]$datetime)
function ConvertLocalToUnix([datetime]$datetime)
Line 447: Line 452:
{
{
     [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($sincepoch))
     [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($sincepoch))
}
</source>
=== IP Address ===
The object structure: http://msdn.microsoft.com/en-us/library/system.net.ipaddress
To create an IP address object...
<source lang="powershell">
$ip = [System.Net.IPAddress]::Parse("192.168.1.123")
</source>
To confirm a supplied address is valid...
<source lang="powershell">
if ( [System.Net.IPAddress]::TryParse($IP2test) ) {
    Write-Host "$IP2test is a valid address"
}
}
</source>
</source>
Line 459: Line 479:


== Macros / Built-in Variables ==
== Macros / Built-in Variables ==
{|class="vwikitable"
{|class="vwikitable"
|-  
|-  
! Variable                !! Description
! Variable                !! Description
|-
|-
| <code> $_ </code>      || Either
| <code> $_ </code>      || Variable passed through pipeline from previous command
|-
|-
| <code> $? </code>      || Success/failure of previous statement - see [[#Basic_Error_Handler|Basic_Error_Handler]]
| <code> $? </code>      || Success/failure of previous statement - see [[#Basic_Error_Handler|Basic_Error_Handler]]
Line 474: Line 493:
| <code> $foreach </code> || Enumerator in a foreach loop
| <code> $foreach </code> || Enumerator in a foreach loop
|-
|-
| <code> $Host </code>    || Information about the machine being executed on
| <code> $Host </code>    || Information about the machine being executed on (can also use <code>Get-Host</code>
|-
|-
| <code> $args </code>    || Array of arguments passed to a script - see [[#Script_Arguments|Script_Arguments]]
| <code> $args </code>    || Array of arguments passed to a script - see [[#Script_Arguments|Script_Arguments]]
|}
|}
=== $env ===
This object contains all local system environment variables.  For example '''<code>$env:COMPUTERNAME</code>''' provides you with the local system's computer name.
For a full list use...
<source lang="powershell">
Get-ChildItem env:
</source>
...or for some commonly used ones...
{|class="vwikitable"
|-
! Variable        !! Description                  !! Example data
|-
| APPDATA          || Application data path        || C:\Users\RodHull\AppData\Roaming
|-
| COMPUTERNAME    || Local System's hostname      || LAPTOP-01
|-
| LOGONSERVER      || Domain Controller Logged into || DC-SERVER-03
|-
| PROCESSOR_ARCHITECTURE    || CPU Architecture    || AMD64
|-
| USERDOMAIN      || Logged in user's domain      || DOMAIN.COM
|-
| USERNAME        || Local System's hostname      || rodhull
|}


[[category:PowerShell]]
[[category:PowerShell]]

Navigation menu