Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Windows PowerShell New-Item

Windows PowerShell Scripting New-Item

PowerShell's New-Item is a versatile cmdlet particularly good for creating files and folders.  New-Item typifies PowerShell's simplicity coupled with power, added to versatility.

Windows PowerShell New-Item Topics

 ♣

PowerShell Pre-requisites and Checklist

In the case of Windows 7 and Server 2008, you don't need to download any extra files, just 'Add Feature' Windows PowerShell.  However, for older operating systems, installing can be confusing because there are different versions of PowerShell for XP, Windows Server 2003 and Vista.  For such legacy systems only, you need to download PowerShell from Microsoft's site.

Once you have installed PowerShell 2.0, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save you buying a text editor.

Example 1: New-Item Folder

The secret of mastering New-Item is to observe the rhythm, go with the flow of, -path -name -type.

# PowerShell New-Item Folder
Clear-Host
$Location = "E:\PowerShell\"
New-Item -path $Location -name "Ezine" -itemType "directory"

Note:  As usual with my -path examples, I introduce a variable mainly to remind you to change its value if you want the script to work on your machine!

Note:  The -itemType parameter can be abbreviated to plain -Type.  Also "Directory" does not have to be in quotes.

Note:  If the script succeeds PowerShell gives you a summary of what the script achieved, however, I still like to open Windows Explorer to checkout my new folder. 

Example 2: New-Item File

# PowerShell New-Item File
Clear-Host
$Location = "E:\PowerShell\Ezine"
New-Item -path $Location -name "No210" -type File

Note:  If your version of the script fails, start troubleshooting with value of $Location.

Note:  That it's a case of New-Item file by name and -type "file" by nature.  By that I mean check your -type (or -itemType) string value.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

Example 3: New-Item Registry Key

# PowerShell New-Item Registry Key
Clear-Host
$RegPath = "HKCU:\Software\GuyTest1"
New-Item -path $RegPath -type Directory

Note: If you are wondering about creating the actual values or leaf items, then seek out the sister cmdlet New-ItemProperty.

Example 4: New-ItemProperty Registry Value

# PowerShell New-ItemProperty Registry Value
Clear-Host
$RegPath = "HKCU:\Software\GuyTest4"
New-ItemProperty -path $RegPath -Name Mosel -PropertyType String -Value "28"

Note: -PropertyType String.   Also note rhythm of the command -path -name, -propertyType and last but not least, -value.

Note: See more on ItemProperty

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

Solarwinds' Orion performance monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is the ability to monitor the health of individual VMWare virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of Solarwinds' offer.

Download a free trial of the Network Performance Monitor.

More New-Item Tutorials

Research New-Item Parameters

Examining New-Item's parameters is particularly fruitful.  It emphasises the variety of objects that you can create, and how you can employ parameters to modify the new article and even give it initial values.

# PowerShell New-Item Parameters
Clear-Host
Get-Help New-Item

Checking the help file may reveal additional useful parameters, for instance -force and -credential.

Alias for New-Item (Ni)

New-Item has an alias of Ni.  In addition, Mkdir or Md mimics New-Item and creates a directory.

With Microsoft, there are always at least three ways of doing everything, what seems like redundancy when you are an expert, seems like perspective when you are a beginner.  One obvious example is that you can abbreviate format-Table to ft.  As you increase your range of PowerShell commands, keep an eye out for another PowerShell Alias, for example gci (Get-Childitem).

Researching Similar PowerShell Cmdlets

# PowerShell Item Cmdlet Research
Clear-Host
Get-Command -noun Item

This reminds me that there is a Remove-Item cmdlet, which can be useful for removing your failed experiments, but go carefully, Remove-Item can lead to disaster, so before use even Gung-ho Guy backs up at least some of the File structure of registry.  PowerShell -noun or -verb research always throws up at least one surprise.

Work in Progress - Investigate

Registry Provider accepts ::
Get-PsProvider

Summary of PowerShell New-Item

Sooner or later everyone needs new stuff.  Whether it's a file, folder or registry key, New-Item can deliver just the article that you need for your PowerShell project.

If you like this page then please share it with your friends

 


See more PowerShell examples for syntax constructions

PowerShell Tutorials  • Syntax  • Pipeline  • Quotes  • New-Item  • Remove-Item  • ItemProperty

Select-String  • -replace string  • Group-Object  • Sort-Object 

Windows PowerShell cmdlets   • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.