Showing posts with label Objects-Variables. Show all posts
Showing posts with label Objects-Variables. Show all posts

August 12, 2014

Don’t Count, Measure

How often do you use the  Count property of an object to get the number of items in it ?

$SomeArray.count

The problem is that if you try to get the  Count property on a null object or on a object that is not a collection or an array it will throw an exception.

The solution is to use the Measure-Object cmdlet :

($SomeArray | measure-object).count

June 28, 2012

Hom to create an hash table

@{
"key1" = "value1"
"key2" = "value2"
}

Ref : Working with Hash Tables

November 4, 2011

How to replace values in a string

Sometimes you a have a string like ”Cannot find file $1” where you want to replace the $1 by a value (ex: myfile.txt). Sure you can use the –replace operator but there is a much more elegant solution : the –format operator.

$ErrMsg="Cannot find file {0}" -f 'myfile.txt'

$ErrMsg will output :
Cannot find file myfile.txt

You can even use the –format in a multi-value, multi-line way :

$ErrMsg=@"
Filename : {0}
Filepath : {1}
Size     : {2:N} MB
"@ -f 'Myfile.txt','C:\temp',(1234567/1MB)

$ErrMsg will output :
Filename : Myfile.txt
Filepath : C:\temp
Size     : 1,18 MB

April 7, 2010

How to create a collection of custom objects ?

First you have to create a collection object :

$psobjectStrongName = (New-Object psobject).psobject.GetType().AssemblyQualifiedName
$Col = New-Object "System.Collections.ObjectModel.Collection``1[[$psobjectStrongName]]"

Than you have to add an item (object) to the collection.

Method 1 :

$colItem = New-Object psobject -Property @{ObjRef=1;AddedBy="New-Object"}
$Col.Add($colItem)
$colItem =$null

Method 2 :

$colItem = New-Object psobject
Add-Member -InputObject $colItem -MemberType noteproperty -Name ObjRef -Value 2
Add-Member -InputObject $colItem -MemberType noteproperty -Name AddedBy -Value "Add-Member"
$Col.Add($colItem)
$colItem =$null

Method 3 (my personnal favorite) :

$Col.add((New-Object psobject -Property @{ObjRef=3;AddedBy="add with New-Object"}))

Source : http://www.vistax64.com/powershell/25307-how-put-psobject-into-system-collections-objectmodel-collection.html

How to create a custom object ?

$obj=New-Object -TypeName Object
$obj|Add-Member NoteProperty MyPropertiy -value "Hello"

or

$$obj = New-Object psobject -Property @{MyURL="http://wow.com";MyRef=1}

How to verify if a variable exists ?

Test-Path variable:\myVariableName

You can also do the following :

if ($myVariableName) {$true} else {$false}

But this will not work if you have used Set-StrictMode -Version 2.0

How to create a constant ?

new-variable DOMAIN_PASSWORD_COMPLEX 1 -option constant

How to use an environment variable

$env:path

How to list all environment variables

get-childitem env:

How to list all variables

Get-variable

How to get the type of a variable ?

$home.gettype()

How to delete a variable ?

$myweb = $null

Or

remove-variable "myweb"

How to use an enumaration type value ?

$a = [System.IO.FileAttributes]::Archive

How to list enumeration type values ?

[enum]::getvalues([System.IO.FileAttributes])

Or you can use this function made my $hay@Israel :

function Get-EnumValues{[enum]::getvalues($args[0]) | select @{n="Name";e={$_}},@{n="Value";e={$_.value__}} | ft -auto } 

What is the difference between single (‘) and double quotes (“) ?

With double quotes (“), variables in the string are expanded.

write-output "Starting engine from $pshome ..."
Will output :
Starting engine from C:\Windows\System32\WindowsPowerShell\v1.0 ...
To expand an expression use $(), ex:
Write-Output "the Powershell host is $($host.name)"