Thursday, November 17, 2011

Use PowerShell as “Calculator”

 

Hi,

this is a very basic tip.

The tip is “You can use PowerShell as a calculator”, so guys stop using CALC use PowerShell instead.

I don’t teach you how to use Addition (+), Subtraction (-), Multiplication (*) and Division (/), tried it yourself :)

CalcuLator

 

Thanks

Aman

Wednesday, November 16, 2011

“VIM” a console based Script Editor for PowerShell

 

Hi,

I hope some of you know about what is VIM is and for those who don’t know what VIM is for them “VIM” is console based text editor and it’s very advance text editor. Basically VIM was made for LINUX and it’s come pre-installed in most of the Linux Distributions. Now they also make it compatible with Windows. You can download VIM executable from VIM website or you can download VIM directly from this Download link ftp://ftp.vim.org/pub/vim/pc/vim73_46w32.zip 

Why to use VIM?

hummm.. if you want to do edit PowerShell scripts directly from PowerShell Console or you don’t want to use EDIT command to edit PowerShell Script and want some more advance features then VIM is for you. We will use VIM as console based test Editor.

How to use it?

Download vim  ftp://ftp.vim.org/pub/vim/pc/vim73_46w32.zip  and save it anywhere on your laptop and Extract the .ZIP file.

Vim-0

Now Paste the extracted Vim.exe folder in C:\Windows\System32 so that it automatically added to your environmental path so that we can execute the VIM.exe from anywhere in the console.

Vim-1

Now lets open PowerShell Console and see if we able to run VIM successfully.

Vim-2

It’s Open.. to quit this windows type :q

Vim-3

Now open a PowerShell Script using VIM , Vim  and location of PowerShell Script

Vim-4

Looks good isn’t

Vim-5

Press Escape and type :se nu in vim and hit Enter and Line Number appear in the script :)

Vim-7

to quit the vim without saving the script Press Escape Key and type :q! and hit enter.

To know more about vim see this online documentation : http://vimdoc.sourceforge.net/htmldoc/usr_toc.html 

 

Thanks for reading

 

Aman Dhally

Tuesday, November 15, 2011

Ping Multiple Servers using “PowerShell”

 

Hi,

Today I was looking for  a little PowerShell script which can Ping multiple servers and give a consolidated view on PowerShell console that which one is Live and which one is Dead.

Then a decide to write my own and finally wrote  it: ) and it is a only a 9 line script :)

You can copy and paste the script from below or you can download it from here : http://dl.dropbox.com/u/17858935/Ping_Multiple_Servers.zip 

let me explain the script :

$Servername: is a variable which contain a list  of comma separated Server name

in foreach script block we are selecting all servers in $ServerName variable and run Test-Connection cmdlets against them

in If Test-Connection evaluates that servers in Pinging that it writes “S$server is alive” otherwise it write “Server is dead”,

In test-connection Cmdlet we define that it should send only 2 Pings to the server using –count 2 switch parameter and used –Quiet Switch parameter so that is show the output in Boolean (true or false)

 

   1:  #### Provide the computer name in $computername variable
   2:   
   3:  $ServerName = "Dc-2","LocalHost","Server-2","Not-Exists","Fake-computer"
   4:   
   5:  ##### Script Starts Here ###### 
   6:   
   7:  foreach ($Server in $ServerName) {
   8:   
   9:          if (test-Connection -ComputerName $Server -Count 2 -Quiet ) { 
  10:          
  11:              write-Host "$Server is alive and Pinging " -ForegroundColor Green
  12:          
  13:                      } else
  14:                      
  15:                      { Write-Warning "$Server seems dead not pinging"
  16:              
  17:                      }            
  18:  }
  19:   
  20:  ########## end of script #######################



The output of the script will be like below link.


Ping


 


Download Link : http://dl.dropbox.com/u/17858935/Ping_Multiple_Servers.zip 


 


Thanks


aman dhally

Monday, November 14, 2011

How to create and change “Windows Registry Keys” using PowerShell.

 

Hi,

The Best thing about PowerShell is that you can manipulate “windows  Registry Keys” very easily with it. How? Let me show to you.

Why we need to create a registry Hive/key manually?

Actually for lots of reason, for example if you want to install some particular software on all accounting computer automatically and then you can need write a script which check the predefined registry Keys and if it found that the in key  Department is “accounts” then install it may install some accounting software on it otherwise don’t do anything.

Cmdlets used: New-Item , New-ItemProperty, Set-ItemProperty

Before running the below command  make sure you run PowerShell as Administrator.

How to run PowerShell as Administrator?

to do this , Search for PowerShell { you can also access the same from Start > All Programs > Accessories > windows PowerShell > Windows PowerShell} , Right Click on it and Choose “Run as Administrator”

Registry-A

When you run PowerShell as Administrator you will see that the title bar of PowerShell windows shows Administrator:

Registry-0

Now let’s create a new Folder named “CompanyName” in “Hkey_Local_machine” and  beneath Software key folder.

In PowerShell we can use Registry hives as Local drives so we can use New-item cmdlet to create a new registry hive.

new-Item -Name "CompanyName"  -Path 'hklm:\SOFTWARE\'  -type Directory

New-Item : used to create a new folder for file

-Name : Name of the Folder

-Path: where we want to create a new folder

-Type: Directory in our case because “CompanyName” will contain some sub registry keys.

Registry-1

Let’s  check . . . Yes “Registry Hive” is created . .

Registry-2

now lets create a new Registry key in “ComanyName”  names as “Department” which have some information about which on Department the laptop/desktop belong to.

New-ItemProperty -Path 'hklm:\software\CompanyName\' -Name "Department" -Value "I.T"

Registry-3

Let’s check ….. Key is created . . . .

Registry-4

okies..

what if you need to change the value of the key?, you can do it easily using Set-ItemProperty cmdlet.

Now Lets change the department name from “I.T.” to “Accounts”

set-ItemProperty -Path 'hklm:\software\CompanyName\' -Name "Department" -Value "Accounts"

 Registry-5

Let’ See…….yes Department name is changes from “I.T.” to “Accounts”

Registry-6

 

I hope this post will save someone time :) ..

Thanks

Aman Dhally

www.amandhally.net/blog

Friday, November 11, 2011

How to create New files using PowerShell.

 

Hi

Yes Yes I know its very Simple, But I love to share :) , I know may be everyone know how to create new files using PowerShell, but still I want to share it :)

Cmdlet to use: New-Item

Let’s start:

the command is :

New-Item -Name "Test.txt" -Path "D:\demo" -ItemType File

New-Item: Cmdlet use to create a new file or Directory

-Name: Name of the file, provide file type by giving them proper file extension for example .txt, .csv, .ppt etc.

-Path: File Location  where you want to save the file

-ItemType : It should be file when you are creating files.

New-file

Our Test.Txt file is successfully  created in D:\Demo

New-file-2

now lets create a new blank MSWORD file using the same command but we changed the file extension .txt to .DOC

New-Item -Name "Test2.doc" -Path "D:\demo" -ItemType File

New-file-3

let’s see the file. , It’s Created

New-file-4

 

What if you want to overwrite an existing file. to do this use –Force switch parameter with the command.

New-Item -Name "Test.txt" -Path "D:\demo" -ItemType File -force

See in the below screenshot, before running the above command the length/size of test.txt was 20 bytes but after running the above command the old file “test.txt” overwritten by new “test.txt” and the new file don’t have any content in it that’s why the new file size is Zero.

New-file-5

I Hope this is Helpful…… at least to someone :)

 

Thanks

Aman Dhally

How to create a New folder using PowerShell.

 

Hi,

I know it is very simple to do :) , but sometime dumb like me don’t know how to do it . Or which Command is used to  do it ..

The Command which is used to create a new folder is : New-Item

You can also create a new folder using old mkdir command: Mkdir “Folder Name”

but let’s use PowerShell way to do this.

Lets Start:

Open PowerShell Console

To create a new Directory use this command  and hit enter:

New-Item -path D:\Demo -Name "Foo" -ItemType Directory

-path:  location path where you want to create folder

-Name : Name of the Folder / directory to be Created

-ItemType: it is “directory” to create a folder

and It creates a folder name “Foo” in D:\Demo

New-Item

what if you want to create a new folder which is already exists?

you will got an error “Directory Name Already exists”

New-Item-2

and If you want to create a new folder and that folder already exists and you want overwrite it, try      –Force switch parameter with the command.

New-Item-3

 

Hope this is helpful . . .

Thanks

Aman Dhally

Wednesday, November 9, 2011

Use “PowerCfg.exe” to change “Power Scheme” of the system with the help of “PowerShell”


Hi,

In my office everyone is using Different “Power Schemes” on there  systems. it’s good but sometime it cause little problems sometime  Somebody complains about his/her desktop go to sleep mode after “10 Minutes” which they don't like , somebody says they “ want to hibernate their laptops after 15 Minutes of idle time.
Then i think that the best way to resolve the problem is to apply  same “Power Scheme” on all the laptops, And to implement  this I choose  “PowerShell”.

The Ideas is :

Write a Script in PowerShell , which check the Current “Power Scheme” and if the scheme is one which we want to use than it wont do anything and if user have some other power scheme applied then the script change it to our one. Then put this script in to “Login Script” so that it applied and check every time when users log-in in to his/her laptop.
To accomplish this task we have used Windows inbuilt command like utility “PowerCfg.exe”

You can download the Script from here : http://gallery.technet.microsoft.com/scriptcenter/Change-Windows-Power-Scheme-3b794e0b

Some Screenshots:
Lets Run the PowerCfg –L to check the available power schemes

PowerCfg-1

Ok.. Now check which “Power Scheme is Currently Active” using PowerCfg –GetActiveScheme  , currently “Maximum Battery Life” Power scheme is active , and in our script we define that default “Power Scheme” should be “ Energy Star”

PowerCfg-2

Let’s RUN the script and see if that works….After running the script it says that “Power Scheme” Successfully applied” ,

PowerCfg-3 

Let’s Verify it., run the PowerCfg –GetActiveScheme  again and see what we got,,, Bingo !!!! now our current  active scheme is “Energy Star” which means our script is running successfully

PowerCfg-4

You can download the Script from here : http://gallery.technet.microsoft.com/scriptcenter/Change-Windows-Power-Scheme-3b794e0b

Thanks
Aman Dhally