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

 ♣

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 1:  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 2:  The -itemType parameter can be abbreviated to plain -Type.  Also "Directory" does not have to be in quotes.

Note 3:  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 4:  If your version of the script fails, start troubleshooting with value of $Location.

Note 5:  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:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network 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.

What I like best is the way NPM suggests solutions to network problems.  Its also has 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 try NPM now.

Download a free trial of Solarwinds’ Network Performance 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 6: 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 7: -PropertyType String.   Also note rhythm of the command -path -name, -propertyType and last but not least, -value.

Note: See more on ItemProperty

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

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 -Full

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

Another example of Remove-Item »

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.  You could also research with -Verb New.

Research More 'New' Cmdlets

I was surprised how many cmdlets started with verb 'New', here is the list which you can obtain with Get-Command -Verb New

Name
—-
New-IseSnippet
New-NetAdapterAdvancedProperty
New-PSWorkflowSession
New-RegKey
New-Alias
New-AppLockerPolicy
New-CimInstance
New-CimSession
New-CimSessionOption
New-Event
New-EventLog
New-Item
New-ItemProperty
New-JobTrigger
New-Module
New-ModuleManifest
New-Object
New-PSDrive
New-PSSession
New-PSSessionConfigurationFile
New-PSSessionOption
New-PSTransportOption
New-PSWorkflowExecutionOption
New-ScheduledJobOption
New-Service
New-TimeSpan
New-Variable
New-WebApplication
New-WebAppPool
New-WebBinding
New-WebFtpSite
New-WebGlobalModule
New-WebHandler
New-WebManagedModule
New-WebServiceProxy
New-Website
New-WebVirtualDirectory
New-WinEvent
New-WSManInstance
New-WSManSessionOption

PowerShell New-ItemProperty »

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   • Remove-Item   • ItemProperty

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

Windows PowerShell cmdlets   • Windows PowerShell  • New-Item  • PowerShell New Object

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.