Saturday, 29 May 2010

Replace-String.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates how to do .REPLACE() in a string. 
  4. .DESCRIPTION 
  5.     This script is a MSDN sample, recoded in PowerShell 
  6. .NOTES 
  7.     File Name  : Replace-String.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/05/replace-stringps1.html 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/fk49wtc1%28VS.80%29.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Replace-String.ps1 
  17.     The original string is: 
  18.     This docment uses 3 other docments to docment the docmentation 
  19.      
  20.     After correcting the string, the result is: 
  21.     This document uses 3 other documents to document the documentation 
  22. #> 
  23.  
  24. ## 
  25. # Start of script 
  26. ## 
  27.   
  28. # Create a string complete with misspelling and display it 
  29. $errString = "This docment uses 3 other docments to docment the docmentation" 
  30. "The original string is:`n{0}" -f $errString 
  31. "" 
  32.  
  33. # Correct the spelling of "document" using .Replace() and display  
  34. # the corrected string 
  35. $correctString = $errString.Replace("docment", "document"
  36. "After correcting the string, the result is: `n{0}" -f $correctstring 
  37. # End of script 

No comments: