Wednesday, 7 October 2009

New-Folder.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script Creates then removes a folder using methods in 
  4.     System.IO.Directory. 
  5. .DESCRIPTION 
  6.     This script checks to see if a staticly named folder exists. 
  7.     if not, it creates then removes the folder. The creation/deletion 
  8.     logic in enclosed within a try/catch block to capture errors. 
  9. .NOTES 
  10.     File Name  : New-Folder.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/54a0at6s.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\New-Folder.ps1' 
  20.     The directory was created successfully at 10/7/2009 12:00:35 PM. 
  21.     The directory was deleted successfully. 
  22. #> 
  23. # Specify the directory to manipulate 
  24. $path = "c:\fooxx" 
  25.   
  26. # try to see if the dirctory exists (It should not!) 
  27. try { 
  28.    if ([System.Io.Directory]::Exists($path)) {   
  29.                 "That path exists already." 
  30.                 return 
  31.     } 
  32.  
  33. # Try to create the directory 
  34. $di  = [System.Io.Directory]::CreateDirectory($path
  35.   
  36. # Get creattion time and display results 
  37. $dit = [System.Io.Directory]::GetCreationTime($path
  38. "The directory was created successfully at {0}." -f  $dit
  39.   
  40. # Delete the directory. 
  41.   $di.Delete() 
  42.   "The directory was deleted successfully." 
  43. }  
  44. catch { 
  45.             "The process failed" 
  46. }  

No comments: