Friday, November 18, 2011

Copy Files and Folders using PowerShell

 

 

Hi,

Copy files and folders this is the task which every Admin or even a end user performs daily.

How we can Copy files and folder using PowerShell?

Quite Easy, we just need to use Copy-Item cmdlet with few Switch Parameters that all.

Cmdlet used : Copy-Item

:::: Copy Files

Lets create a a new folder to Backup files currently I have  one Folder named “Folder_A” which contain Data and we will create a new folder named as “Backup_A” , and then copy data from “Folder_A” to “backup_A”

Files-1

let create a new folder named “Backup_A”

Files-2

Now we have two folders “Folder A” and “Backup_A”

Files-3

now copy a file from “Folder_A” to “backup_A” ,

command is: Copy-Item D:\Demo\Folder_A\Book.txt -Destination D:\Demo\Backup_A

file successfully copied.

Files-4

:::: Copy Txt file only

-Include

if we want to copy specific type of files only then we can use –Include switch Parameter followed by the type .

-Verbose 

We can use –Verbose switch to see which files are getting copied

command: Copy-Item D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Include *.txt –Verbose

see the screenshot, command copied the .Txt files only

Files-5

:::: Exclude files to copying

-Exclude

If we are using Copy-Item cmdlet in a backup script it would be good we don’t backup Music,Video files on server. As a Ideal Policy we should not fill our SAN, Server storage with the backup of users music and videos.

to exclude these files to be copied use –Exclude switch parameter followed by file types example *.mp3,*.mp4 etc

in my Folder_A i have few MP3 audio and few MP4 video files, lets stop them copying to our Backup_A folder.

Files-6

note: * means all

Command: Copy-Item D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Exclude *.mp3,*.mp4

When i search for *mp3 and  *mp4 no result were found and you can also see there is not a single MP3 and MP4 in “Backup_A” folder.

Files-7

:::: Copy Folders

the command is same like copying file but rather then specify file we need to specify folder

Command : Copy-Item D:\Demo\Folder_A\Recurse_Testing -Destination D:\Demo\Backup_A\

Folder Copied

Files-8

BUTTTTTTTT !!! wait…!!!!!!!!!!!!!

Check the folder again, Recurse_Testing have some Sub-folders which are not copied in backup_A

Files-9

-Recurse

to Copy All folder including sub-folders use –Recurse while copying

Command is:

Copy-Item -Recurse D:\Demo\Folder_A\Recurse_Testing -Destination D:\Demo\Backup_A\  -Verbose

Files-10

let check if it copied subfolder also.

Seems Identical isn’t.

Files-11

:::: Copy all files and Folders

To copy all file and folder we use * wildcard which means all , and –Recurse to copy sub-directories also and we also use –force do that if there any file exists with same name on backup folder it should get overwrites.

command : Copy-Item -Recurse D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Force –Verbose

Files-12

lets check if these two folders are identical.

Seems like TWINS isn't ;-)

Files-13

Hope it is useful to someone .

Thanks

Aman Dhally

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