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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.