Wednesday 3 October 2012

Powershell Tips

How to change the element of an array?

$array = 1, 2,3, 4, 5

$array.Item(0) = 100

$array

How to sort an array ?

Use the static sort method from array class

$a = 1,3, 4,2

[array]::sort($a)



How to display Loaded and available Modules in Powershell?

Use the Get-Module cmdlet to display loaded

To see the path of the module

Get-Module | select path

To list the modules,

Get-Module -Listavailable

How to open a pipeline results in editor?

 dir | Out-File result.txt; notepad result.txt


How to list all parameters of parametersets?

PS C:\Users\sankar> (Get-Command Get-Date).parametersets | foreach {$_.parameters} | foreach {$_.name}



How to check the folder counts ?

We can find the number of files available on desktop this way


PS C:\Users\sankar\Desktop> @(get-childitem -name *).count
35

In case if you want to exclude some files when counting.

 @(Get-ChildItem -exclude *.txt).count

How to exclude the files from displaying?

Get-ChildItem -exclude *.txt

How to get all the child items from the container that has child items?

We should use  -Recurse Parameter if we need to get all the child items from the container

get-childitem -Recurse $env:USERPROFILE\desktop

we can use -filter if we want to filter with some specified extension file like *.txt , *.docx etc

get-childitem -Recurse $env:USERPROFILE\desktop -filter *.txt

How to check the version of Powershell and .Net Framework?


PS C:\Users\sankar\Desktop> $PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.5456
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

To check version for .Net Framework


PS C:\Windows\Microsoft.NET\Framework> dir


    Directory: C:\Windows\Microsoft.NET\Framework


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          3/3/2012   8:27 AM            v1.0.3705
d----         7/13/2009  10:20 PM            v1.1.4322
d----          8/4/2012   9:22 AM            v2.0.50727
d----         2/17/2011   9:25 PM            v3.0
d----          3/3/2012   8:27 AM            v3.5
-a---         11/4/2010   6:57 PM      86864 NETFXSBS10.exe
-a---          3/3/2012   2:50 AM      41392 netfxsbs12.hkf
-a---         6/10/2009   4:22 PM      13648 sbscmp10.dll
-a---         6/10/2009   4:22 PM      13648 sbscmp20_mscorwks.dll
-a---         6/10/2009   4:22 PM      13648 sbscmp20_perfcounter.dll
-a---         6/10/2009   4:22 PM      11104 sbs_diasymreader.dll
-a---         6/10/2009   4:22 PM      11088 sbs_iehost.dll
-a---         6/10/2009   4:22 PM      11112 sbs_microsoft.jscript.dll
-a---         6/10/2009   4:22 PM      11656 sbs_microsoft.vsa.vb.codedomprocessor.dll
-a---         6/10/2009   4:22 PM      11096 sbs_mscordbi.dll
-a---         6/10/2009   4:22 PM      11096 sbs_mscorrc.dll
-a---         6/10/2009   4:22 PM      11096 sbs_mscorsec.dll
-a---         6/10/2009   4:22 PM      11120 sbs_system.configuration.install.dll
-a---         6/10/2009   4:22 PM      11088 sbs_system.data.dll
-a---         6/10/2009   4:22 PM      11112 sbs_system.enterpriseservices.dll
-a---         6/10/2009   4:22 PM      11080 sbs_VsaVb7rt.dll
-a---         6/10/2009   4:22 PM      11104 sbs_wminet_utils.dll
-a---         6/10/2009   4:22 PM      13648 SharedReg12.dll

To Create New Item Using Powershell

C:\Users\Sankar> New-Item <File Name> -ItemType File


PS C:\Users\sankar\Desktop> 1..5 | %{New-Item
-Name "$_.txt"  -Value (Get-Date).tostring() -ItemType file}

NULL Value and Parameter Type


PS C:\> Get-ChildItem $env:windr | ?{$_.length -eq $null}


    Directory: C:\


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         7/17/2012   2:11 PM            Intel
d----         7/14/2009   8:07 AM            PerfLogs
d-r--        11/12/2012   1:53 PM            Program Files
d----         8/28/2012   2:43 AM            Quarantine
d----         7/18/2012  12:02 AM            source
d-r--        10/17/2012   8:25 PM            Users
d----         12/7/2012   3:50 PM            Windows


function NULLValue
{
  param
  (
    [string]$a = $( throw "Missing: parameter a"),
    $b = $( throw "Missing: parameter b")
  )

  if ($a -eq $null) { Write-Host "a is null" } else { write-Host "a is not null" }
  if ($b -eq $null) { Write-Host "b is null" } else { Write-Host "b is not null" }
}

If I call this function with: NULLValue $null $null

I will get the following result:

a is not null
b is null

How to round the number?

Use static Round method using math Class as mentioned below