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.  
  32. Try 
  33.   { 
  34. # Create then remove directory 
  35.      $result = [System.IO.Directory]::CreateDirectory($Path) 
  36.      "Created: $path"    
  37.      [System.IO.Directory]::Delete($Path) 
  38.      "Deleted: $path" 
  39.  
  40. # Check existance for top and sub dirs then display results 
  41.      $directoryExists    = [System.Io.Directory]::Exists("C:\NewDirectory") 
  42.      $subDirectoryExists = [System.Io.Directory]::Exists($Path) 
  43.      "Top-level directory exists:  $directoryExists" 
  44.      "Sub-directory exists      :  $subDirectoryExists" 
  45.    } 
  46. Catch  
  47.    { 
  48.        "The process failed: {0}" -f $($error[0].Message) 
  49.    }    

No comments: