Wednesday, February 29, 2012

Using the -F format Operator in PowerShell

 

Hi,

Yesterday I was using –F format Operator in PowerShell.  In the first view it seems like puzzle me to but after spending few hours working on it start seems simple.

-F format operator is basically a .NET based.

Why we need to use the –F format operator?   

Yes we have few cmdlets by using them we can format the output, but by using –F format operator we can do more than that.

SYNTAX

The syntax for –F format operator is

{<index>[,<alignment>][:<formatString>]}

 

 

 

Format Strings

Description

C

Currency

X

Display Number in Hexa Decimal

p

Display Number as Percentage

n

Display with width n to left side

-n

Display with width –n to right side

dn

Display Numbers Padded by 0 by n times

#

Digit placeholder,

,

Thousand separator

\

Escape Character

:ddd

Day of Week

:dd

Day of Month

:dddd

Full name of Day of Week

:hh

Hour

:HH

Hour in 24 Hour format

:mm

Minutes

:SS

Seconds

:MM

Month in Number

:MMMM

Name of the Month

:yy

Year in short

:yyyy

Full year

In –F format operator we provide the strings and numbers in the right hand side and stantax for –F in left hand side.

Position

In below example we are just querying the first number in our right hand side. The first number is 1,

"{0}" -f 1,2,3,4,5

clip_image001

Ok now let’s add some space in our first number.

"{0,10}" -f 1,2,3,4,5

You can see that the Position of  “1” is moved to little bit on right hand side.

clip_image002

 

·         :c  or :C | Currency

"{0:C}" -f 200,99,765

You can see that now the output is in currency.

clip_image003

Let’s choose the second number

"{1:C}" -f 200,99,765

clip_image004

·         :x or :X | Hexa Decimal

"{0:x}" -f 909887

It Converted the Number 909887 to hexadecimal value.

clip_image005

 

·         :p | Percentage

"{0:p}" -f .703

Output is in Percentage.

clip_image006

 

·         :dn | Padded 0

"{0:d7}" -f 9

We specified to add 7 zeros but it added 6 , actually it added 7 and the last 0 replaced by the decimal number.

 

clip_image007

 

·         # | Digit Place Holder

"{0:###-##-##}" -f 8976203

When we use # in format operator the # are replaced by the digits.

clip_image008

 

·         Date and Time Formater

 

"{0:ddd}" -f (get-date)

"{0:dd}" -f (get-date)

"{0:dddd}" -f (get-date)

"{0:hh}" -f (get-date)

"{0:mm}" -f (get-date)

"{0:MM}" -f (get-date)

"{0:MMMM}" -f (get-date)

"{0:yy}" -f (get-date)

"{0:yyyy}" -f (get-date)

clip_image009

·         ,| Thousand separator

"{0:#,#}" -f 100980243

clip_image001

 

·         Practice Example

$pro = Get-Process

foreach ($a in $pro) {

"{0,20} {1,40}" -f $a.ProcessName , $a.vm

}

Try the above script and the output should be like this .

clip_image001 

USEFUL Links :               

http://msdn.microsoft.com/en-us/library/26etazsy.aspx

Thanks

Aman Dhally

Tuesday, February 28, 2012

List of useful WMI classes to use with PowerShell.

 

Hi,

Here is the list of some useful WMI Classes :).

ClassName

Contains

win32_ComputerSystemProduct

Serial Number, Vendor, information

win32_bios

Bios Information , including Version Number of BIOS

win32_battery

Battery Information

win32_Physicalmemory

Serial Number, Capacity, Part Number of Installed Memory Stick.

win32_DiskDrive

Capacity, Serial Number of Drive and other info of the Hard-disk

win32_DesktopMonitor

Monitor Information including Resolutions

win32_cdromdrive

Information Related Cd Drive

win32_networkadapter

Network Adaptor information contains, manufacturer, MAC ID etc

win32_pointingdevice

Mouse related information

win32_operatingsystem

OS Name, OSArchitecture, Version Info

win32_logicalDisk

DeviceID, Free Space, Size of Partition,

Win32_NetworkConnection

Mapped Network Drives

win32_printer

List of Installed Pri ters

win32_PrinterdRIVER

List of Printer Drivers

Win32_NetworkAdapterConfiguration

IP Adress, DHCP , DNS and other information of Network Drivers.

win32_startupCommand

List of Software which are on STARTUP folders

win32_process

All Running Processes

win32_Service

List of All Services

win32_Product

List of Installed Software

 

Thanks

aman dhally

Thursday, February 23, 2012

Using BEEP sound in PowerShell Scripts.


Hi,

After watching the valentine post of “Jeffery Hicks” I started playing with $([char]) characters. And while working with these characters I found a great character that is character 7.

23-02-2012 12-07-13

So what is interesting in character number 7?, it is a Beep Sound. For example if  in your script you are rebooting the server or laptop we can provide 3 beeps for rebooting. Or  we can  set a beep after every task or command finished his task.
Cool isn’t..

try this script.

Download Script from Here: http://gallery.technet.microsoft.com/scriptcenter/Using-BEEP-sound-in-f637bb48
#### save it as beep.ps1 or so....
Write-Host "I am going to reboot the Laptop after 3 Beep" -ForegroundColor Red
$([char]7)
write-Host "Beep 1 " -ForegroundColor Green
sleep 1
$([char]7)
write-Host "Beep 2 " -ForegroundColor Green
sleep 1
$([char]7)
write-Host "Beep 3 " -ForegroundColor Green
sleep 1
Write-Host "`n"
Restart-Computer -Force -WhatIf
#################################

and the output is like below. Our computer is going to reboot after 3 beeps...

23-02-2012 12-09-26

Download Script from Here: http://gallery.technet.microsoft.com/scriptcenter/Using-BEEP-sound-in-f637bb48

Thanks for reading,
Aman Dhally
messenger_freak_adp1 (30)

Thursday, February 16, 2012

Set Computer Name using PowerShell.


Hi,

Yesterday I faced a problem in which I need to change the computer name using PowerShell. First I was looking for some cmdlet which can do this job for me but there is not any inbuilt cmdlet for it.

Then I searched and finally find a way by which I can do this within the PowerShell. This method is using WMI to do the task.

ComputerName
Download the script from Here : http://gallery.technet.microsoft.com/scriptcenter/Set-Computername-using-9cb6ecc4

Get-WmiObject Win32_ComputerSystem

We are using Get-WMI to give us  all the value of Win32_ComputerSystem.

You can see in the Output that the "Name value contain our Computer name "WINANAL-088y8gx" with mach with our above screenshot.

Capture-2 

$computerName = Get-WmiObject Win32_ComputerSystem


We are putting the command in to the $computerName variable.  And you can see that the output of $computerName is same and above screenshot in which we run the command alone.

Capture-3

$name = Read-Host -Prompt "Please Enter the ComputerName you want to use."

In $name variables we are using Read-Host cmdlet with -Prompt argument with asking users to provide a computer name to be use.

Capture-4

Whatever user provide to Read-Host, the value is stored in the  $name variable. In First example in below screenshot we didn't provide anything to Read-Host and you can see that output of $name is blank.

 In next example we provide "Test-Laptop" to Read-Host and you can see that now $name contains the value of "Test-Laptop"

Capture-5

$computername.Rename($name)

The Win32_ComputerSystem WMI class contain the method of .Rename() in which we need to provide a desired computer name in brackets.

Capture-6

Capture-7

Now check if you computer name get changed or not.

Bingo !!! Yes It is changed..  now reboot you laptop ;) using  restart-Computer

Capture-8

And if you want a GUI box to to open a Pop-UP to insert computer Name try this
 [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Desired Computer Name ")
$name

Download the script from Here : http://gallery.technet.microsoft.com/scriptcenter/Set-Computername-using-9cb6ecc4

Thanks for reading .
Aman Dhally
aafa005