One Step Further: Deleting Multiple Folders


In this exercise, we will do the following:

  1. Open the yournameimage from book CreateMultipleFolders.ps1 in Notepad or your favorite script editor. This script was created in the step-by-step exercise.

  2. 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}

  3. 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}

  4. 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}

  5. This concludes this one step further exercise. Save your script as yournameimage from book DeleteMultipleFolders.ps1. Run your script. You should see the 10 previously created folders deleted. If you do not, compare your results with the image from book DeleteMultipleFolders.ps1 script in the scripts folder for this chapter.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net