Monday 1 October 2012

Show-FolderCreation.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This example shows how to create a new directory and  
  4.     subdirectory, and then delete only the subdirectory. 
  5. .DESCRIPTION 
  6.     This sample is a re-write of an MSDN Sample,  
  7.     but in PowerShell. The sample firsts creates then removes 
  8.     a folder then looks to see what is left. The target 
  9.     folder is removed, but intermediate folders remain. 
  10. .NOTES 
  11.     File Name  : Show-FolderCreation.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     MSDN sample posted to: 
  18.          http://msdn.microsoft.com/en-us/library/62t64db3.aspx 
  19. .EXAMPLE 
  20.     Psh>  Show-FolderCreation.ps1 
  21.     Created: C:\NewDirectory\NewSubDirectory 
  22.     Deleted: C:\NewDirectory\NewSubDirectory 
  23.     Top-level directory exists:  True 
  24.     Sub-directory exists      :  False  
  25. #> 
  26.  
  27. [CmdletBinding()] 
  28. Param ( 
  29. $Path = "C:\NewDirectory\NewSubDirectory" 
  30.  
  31. Try 
  32.   { 
  33. # Create then remove directory 
  34.      $result = [System.IO.Directory]::CreateDirectory($Path
  35.      "Created: $path"    
  36.      [System.IO.Directory]::Delete($Path
  37.      "Deleted: $path" 
  38.  
  39. # Check existance for top and sub dirs then display results 
  40.      $directoryExists    = [System.Io.Directory]::Exists("C:\NewDirectory"
  41.      $subDirectoryExists = [System.Io.Directory]::Exists($Path
  42.      "Top-level directory exists:  $directoryExists" 
  43.      "Sub-directory exists      :  $subDirectoryExists" 
  44.    } 
  45. Catch  
  46.    { 
  47.        "The process failed: {0}" -f $($error[0].Message) 
  48.    }    

No comments: