How to split string by string in Powershell

The -split operator uses the string to split, instead of a chararray like Split(): If you want to use the Split() method with a string, you need the $seperator to be a stringarray with one element, and also specify a stringsplitoptions value. You can see this by checking its definition: EDIT: As @RomanKuzmin pointed out, -split splits using regex-patterns by default. So be aware … Read more

How to handle command-line arguments in PowerShell

You are reinventing the wheel. Normal PowerShell scripts have parameters starting with -, like script.ps1 -server http://devserver Then you handle them in param section in the beginning of the file. You can also assign default values to your params, read them from console if not available or stop script execution: Inside the script you can simply since all parameters … Read more

Change directory in PowerShell

Unlike the CMD.EXE CHDIR or CD command, the PowerShell Set-Location cmdlet will change drive and directory, both. Get-Help Set-Location -Full will get you more detailed information on Set-Location, but the basic usage would be By default in PowerShell, CD and CHDIR are alias for Set-Location. (Asad reminded me in the comments that if the path contains spaces, it must be enclosed in quotes.)

PowerShell and the -contains operator

The -Contains operator doesn’t do substring comparisons and the match must be on a complete string and is used to search collections. From the documentation you linked to: -Contains Description: Containment operator. Tells whether a collection of reference values includes a single test value. In the example you provided you’re working with a collection containing just one string … Read more

PowerShell says “execution of scripts is disabled on this system.”

If you’re using Windows Server 2008 R2 then there is an x64 and x86 version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts? As an Administrator, you can set the execution policy by typing this into your PowerShell window: For more information, see Using the Set-ExecutionPolicy Cmdlet. When you are … Read more

Echo equivalent in PowerShell for script testing

There are several ways: Write-Host: Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set. Write-Debug: Write directly to the console, if $DebugPreference set to Continue or Stop. Write-Verbose: Write directly to the console, if $VerbosePreference set to Continue or Stop. The latter is intended for extra optional information, Write-Debug for debugging (so … Read more

Echo equivalent in PowerShell for script testing

There are several ways: Write-Host: Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set. Write-Debug: Write directly to the console, if $DebugPreference set to Continue or Stop. Write-Verbose: Write directly to the console, if $VerbosePreference set to Continue or Stop. The latter is intended for extra … Read more