PowerShell Basics: Using New-Item to create Folders and Files

PowerShell Basics_ Using New-Item to create Folders and Files

Windows PowerShell Script to Create Folders

PowerShell is great for rustling up a quick script that will build folders.  You can even add code to create new files once that folder exists.

Our PowerShell Mission to Create a Folder

Our mission is straightforward, to create a directory using PowerShell’s New-Item.

My learning agenda for PowerShell is as follows:
1) To appreciate the -ItemType (-Type) and -Path parameters.
2) To check if the folder or file exists before New-Item goes to work.

How to Creates a Folder Using PowerShell’s New-Item

The secret of mastering New-Item is to observe the rhythm of its parameters – go with the flow of: -Path -name -type.

# PowerShell's New-Item creates a folder
$Location = "D:\PowerShell\"
New-Item -Path $Location -Name "Ezines" -ItemType "directory"
#Invoke-Item $Location

Note 1: As with all my -Path examples, I introduce a variable ($Location) 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 because it does not contain spaces.

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, this is why I have left Invoke-Item on the last line, albeit # remmed out.

Directory: D:\PowerShell

Mode LastWriteTime   Length   Name
---- ------------------  ------------ ----
d---- 18/04/2013 16:16        Ezine

How to Create a File with PowerShell’s New-Item

Assumption: You have an existing folder with the name specified by the $Location variable.  Incidentally, many of the existing cmdlets for example, Out-File, create a file automatically.

# PowerShell Example to create a new file
Clear-Host
$Location = "D:\PowerShell\Ezines"
New-Item -Path $Location -Name "No210.txt" -ItemType File

Note 4: The crucial difference for creating a file is that we use a different value for -ItemType.

Challenge: Try appending Invoke-Item $Location

Testing If $Location Exists

What happens if you run the New-Item script again?  Well you get an error message.  There are a number of ways of improving on just ignoring the red writing, here is script that uses Test-Path, and only creates a file or folder, if one of that name does NOT already exist.

# PowerShell checks, then creates a file and folder
Clear-Host
$Location = "D:\PowerShell\"
$LocationFile = "D:\PowerShell\Ezines"
If((Test-Path $Location) -eq $False) {
New-Item -Path $Location -name "Ezines" -ItemType "directory"
} # End of folder exists test
If((Test-Path $LocationFile) -eq $False) {
New-Item -Path $Location -Name "No212.txt" -ItemType File
} # End of file exist test
Else {"The $LocationFile is already there."
}

Note 5: Naturally, you need to change the values for $Location and $Location; you may even think of better names for these variables.

Get-Help Investigates Parameters of New-Item

It’s always worth researching the switches or -parameters of a newly discovered cmdlet.  All we need is to employ the built-in Get-Help thus:

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

Note 6: I had not realized that New-Item had a -Force parameter to cater for overwriting read-only directories.  In this connection you may also need the -Credential parameter.

See more on PowerShell’s New-Item »

Guy Recommends:  Network Performance Monitor (FREE TRIAL)Review of Orion NPM v11.5

SolarWinds Network Performance Monitor (NPM) 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 on a 30-day free trial.

SolarWinds Network Performance Monitor Download 30-day FREE Trial

Research Similar Cmdlets

The key cmdlet is Get-Command; I like to filter with -Noun Item, or -Verb New to investigate other relatives of New-Item.

Clear-Host
Get-Command -Noun Item 

Name
----
Clear-Item
Copy-Item
Get-Item
Invoke-Item
Move-Item
New-Item 
Remove-Item
Rename-Item
Set-Item

Challenge: Research with: Get-Command -Verb New

PowerShell Remove-Item

When testing you may wish to employ Remove-Item to delete the folder so that you could run the script again.  To save using Windows Explorer, here is how you could zap the folder and file that you just created.

#Warning: Know what you are doing before using Remove-Item
Clear-Host
$Location = "D:\PowerShell\Ezines"
Remove-Item $Location

Note 7: If you don’t use the -Recurse parameter you will be warned if the folder contains files.

PowerShell Remove-Item »

Summary of Creating Files and Directories with PowerShell

It’s easy to employ PowerShell to rustle-up a quick script that will build folders.  You can even add code to create files in that folder.  I have also added ‘If’ statements to check that the folder exists before attempting New-Item.


See more Microsoft PowerShell tasks:

PowerShell Home   • Shell Application   • New-Object   • PowerShell Add Printer   • PowerShell -com

PowerShell Logon Script  • Map Network Drive  • PowerShell Create Shortcut  • Free CSV Import Tool

Invoke-Expression   • Invoke-Command   • Invoke-Item   • PowerShell Expression v Command Mode

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.