PowerShell String
14 JulyPowerShell Remove Special Characters from String
Using Regex.Replace() Method Use the Regex.Replace() method to remove special characters from the specified string in PowerShell. $str = "How #$%are ^&*you?@!" $cleanedStr = [System.Text.RegularExpressions.Regex]::Replace($str, '[^\w\s]', '') Write-Host @" Original String: $str Cleaned String: $cleanedStr "@ Original String: How #$%are ^&*you?@! Cleaned String: How are you First, we declared a variable named $str and set […]
29 JunePowerShell Trim String After Character
Using Split()Method Use the Split() method to trim the string after the first occurrence of the specified character in PowerShell. $string = "Hi! Welcome to Java2Blog! Let's learn." $newString = ($string.Split("!"))[0] Write-Output $newString Hi We declared and initialized the $string variable with "Hi! Welcome to Java2Blog! Let's learn." value. Next, we chained the Split() method […]
07 JunePowerShell Split String by Multiple Delimiters
Using -split Operator with Regular Expression Use the -split operator with a regular expression to split string by multiple delimiters in PowerShell. $string = "one,two;three four" $delimiters = "[,; ]+" $parts = $string -split $delimiters $parts one two three four First, a string variable named $string and an array variable called $delimiters is defined. Then, […]
28 MayConcatenate String and Variable in PowerShell
Using Concatenation Operators Use concatenation operator represented by + to concatenate String and variable in PowerShell. $variable = "together" $concatenatedStr = "Let's learn " + $variable Write-Host $concatenatedStr Let's learn together The + is the most basic concatenation operator; we used it to concatenate the "Let's learn "string with the $variable and stored the new […]
27 MayPowerShell Add Quotes to String
Using Backtick Characters Use backtick characters to add double quotes to string in PowerShell. $string = "Hello `"World`"" $string Hello "World" Backtick character is escape character in PowerShell. Here, it is used to escape double quotes in the String. Use backtick characters to add single quotes to string in PowerShell. $string = "Hello `'World`'" $string […]
25 AprilPowerShell Check if List Contains String
Using -Contains Operator Use the -Contains operator to check if the list contains the specified string in PowerShell. $list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") $searchString = "Wednesday" $list -Contains $searchString True In the above example, we used an array operator represented with @() to create a list, which we stored in the […]
24 AprilPowerShell Split and Get Last Value
Using Split() Method Use the Split() method with the indexing operator to split the specified string and get the last value in PowerShell. $string = "This is a sample string" $lastElement = ($string.Split(" "))[-1] Write-Host "The last element of '$string' is '$lastElement'" The last element of 'This is a sample string' is 'string' First, we […]
22 AprilRun String as Command in PowerShell
Using Invoke-Expression Cmdlet Use the Invoke-Expression cmdlet to run a string as a command in PowerShell. Invoke-Expression cmdlet evaluates string as command and return the result of string expression. Here is simple exampple: $command = "notepad.exe" Invoke-Expression $command This will open new notepad window. Here is more complicated example. $command = "Get-Process | Sort-Object -Property […]
22 AprilPowerShell Check if String Starts with
Using StartsWith() Method Use the StartsWith() to check if string starts with another string in PowerShell. $text = "Hello, world!" $prefix = "Hello" $startsWithPrefix = $text.StartsWith($prefix) Write-Host "Text: $text" Write-Host "Prefix: $prefix" Write-Host "Does '$text' start with '$prefix'? $startsWithPrefix" Text: Hello, world! Prefix: Hello Does 'Hello, world!' start with 'Hello'? True In the above code, […]