
You should see 10 folders created in the C:\Mytest directory. Save your script as yourname CreateMultipleFolders.ps1.

Because this script uses a do …until loop and the value of $i is incremented before entering the until evaluation, the value of $i is always 1 more than the number of folders created. The reason for adding 1 to $intFolders is so the script will actually create the same number of folders as are contained in the $intFolders variable. The condition that until will evaluate is if the $i variable is equal to the value contained in the $intFolders variable + 1. The code that does this is shown here:Ĭlose the script block for the else clause and add the until statement. To do this, use the double plus symbol (++) operator. Increment the value of the $i variable by 1. Copy the New-Item line of code from the if statement and delete the $intPad variable from the name argument. The else script block is the same as the if script block, except it does not include the 0 in the name that comes from the $intPad variable. New-item -path c:\mytest -name $strPrefix$intPad$i -type directory}Īdd the else clause.

The name of the new folder will comprise the $strPrefix constant "testFolder", the number 0 from the $intPad variable, and the number contained in the $i variable. The new folder will be created in the C:\Mytest directory. Use the New-Item cmdlet to create a new folder. Assign the value of 0 to the variable $intPad. Open the script block for the if statement. The condition to be evaluated is if the variable $i is less than 10. This code is shown here:īegin an if … else statement. Include the opening curly bracket for the script block. New-Variable -Name strPrefix -Value "testFolder" -Option constant Use the option argument to make $strPrefix into a constant. Use the value argument of the cmdlet to assign the value of "testFolder" to the variable. Use the New-Variable cmdlet to create a variable named strPrefix. This code is shown here:Ĭreate a variable called $i and put the number 1 in it. The code to do this is shown here:Ĭreate a variable called $intPad.

Open Notepad or your favorite Windows PowerShell script editor.Ĭreate a variable called $intFolders and have it hold the value of 10. If you do not have this folder on your machine, you can either create it manually or modify both the step-by-step exercise and the one step further exercise to use a folder that exists on your machine. In this exercise, we explore the use of constants, variables, concatenation, decision making, and looping as we create 10 folders in the C:\Mytest directory.
