PS: String/Text Replacement

PS supports a built-in string search and replacement operator named –replace. The -replace operator uses a regular expression to search-and-replace through a string. The default behavior is it does a case insensitive similar to that of using –ireplace.  For a case sensitiveness, you need to use –creplace. Default replace behavior: PS C:> $str = "This is a SAMPLE string" PS C:> write-host $Str This is a SAMPLE string PS C:> $str -replace "SAMple","Test" This is a Test string PS C:> write-host $Str This is a SAMPLE string PS C:> Explicit case-insensitive replacement: PS C:> write-host $Str This is a SAMPLE […]

Read more

PS: String Data Mining

One thing that separates Windows PowerShell from other shells (in particular the typical Unix shell) is this: while most operating system shells are text-based, Windows PowerShell is object-based. As you might expect, there are pros and cons to these two different approaches; as a general rule, however, it’s fair to say that Windows PowerShell requires much less text and string manipulation than its fellow operating system shells. So does that mean that you never have to do text and string manipulation in Windows PowerShell? No, sorry: text and string manipulation is still required for anyone writing system administration scripts. Fortunately, […]

Read more

PS Script to Map a Drive with Free Drive Letter in Descending Order

In various cases, you’d like to have a floating drive (created by subst) created with a XenApp User session and you’d need to overcome the drive letter conflict while setting up this.  The easier option is to start checking the drive availability in the descending order and map the drive to a free letter. The preferable option is to have this done at the end of a user logon script so that you’ll get to the free drive letter to be used. Here is a quick PS code that can help you achieve this. PS Script: foreach ($letter in [char[]]([char]’Z’..[char]’A’)) […]

Read more