In this exercise, we will do the following:
Open the yourname CreateMultipleFolders.ps1 in Notepad or your favorite script editor. This script was created in the step-by-step exercise.
In the if … else statement, the New-Item cmdlet is used twice to create folders in the C:\Mytest directory. We want to delete these folders. To do this, we need to change the New-Item cmdlet to the Remove-Item cmdlet. The two edited script blocks are shown here:
{$intPad=0 remove-item -path c:\mytest -name $strPrefix$intPad$i -type directory} else {remove-item -path c:\mytest -name $strPrefix$i -type directory}
The Remove-Item cmdlet does not have a name argument. Therefore, we need to remove this argument but keep the code that creates the folder name. We can basically replace -name with a backslash, as shown here:
{$intPad=0 remove-item -path c:\mytest\$strPrefix$intPad$i -type directory} else {remove-item -path c:\mytest\$strPrefix$i -type directory}
The Remove-Item cmdlet does not take a -type argument. Because this argument is not needed, it can also be removed from both Remove-Item statements. The revised script block is shown here:
{$intPad=0 Remove-item -path c:\mytest\$strPrefix$intPad$i} else {Remove-item -path c:\mytest\$strPrefix$i}
This concludes this one step further exercise. Save your script as yourname DeleteMultipleFolders.ps1. Run your script. You should see the 10 previously created folders deleted. If you do not, compare your results with the DeleteMultipleFolders.ps1 script in the scripts folder for this chapter.