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. [crayon-697eefb8a7cfc326781685/] [crayon-697eefb8a7d04434599750/] First, we declared a variable named $str and set its value to a string containing some special characters. Then, we used the Replace() method of the Regex class from the System.Text.RegularExpressions namespace to replace the special […]
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. [crayon-697eefb8a84b3605152612/] [crayon-697eefb8a84bc683130522/] We declared and initialized the $string variable with "Hi! Welcome to Java2Blog! Let's learn." value. Next, we chained the Split() method with the $string variable to split the $string into an array based […]
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. [crayon-697eefb8a86e7993125726/] [crayon-697eefb8a86ec961375487/] First, a string variable named $string and an array variable called $delimiters is defined. Then, the $string is assigned with "one,two;three four", while $delimiters is set with "[,; ]+". After that, […]
28 MayConcatenate String and Variable in PowerShell
Using Concatenation Operators Use concatenation operator represented by + to concatenate String and variable in PowerShell. [crayon-697eefb8a88c3645138121/] [crayon-697eefb8a88c7704605121/] The + is the most basic concatenation operator; we used it to concatenate the "Let's learn "string with the $variable and stored the new string in the $concatenatedStr variable, which we further used with Write-Host cmdlet to […]
27 MayPowerShell Add Quotes to String
Using Backtick Characters Use backtick characters to add double quotes to string in PowerShell. [crayon-697eefb8a8c71663830870/] [crayon-697eefb8a8c76568889388/] 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. [crayon-697eefb8a8c77240437663/] [crayon-697eefb8a8c78906286042/] Using String Concatenation Operator Use string concatenation operator […]
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. [crayon-697eefb8a8f6e190680035/] [crayon-697eefb8a8f73174003952/] In the above example, we used an array operator represented with @() to create a list, which we stored in the $list variable. Next, we initialized the $searchString variable with a string value we wanted […]
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. [crayon-697eefb8a9381234037458/] [crayon-697eefb8a9384472923962/] First, we defined a $string variable and initialized it with a string value, as shown in the above code fence. Next, we used the Split() method, passed whitespace as a […]
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: [crayon-697eefb8a95aa761866224/] This will open new notepad window. Here is more complicated example. [crayon-697eefb8a95ad275456540/] [crayon-697eefb8a95ae708531232/] The Invoke-Expression evaluates a string as a PowerShell […]
22 AprilPowerShell Check if String Starts with
Using StartsWith() Method Use the StartsWith() to check if string starts with another string in PowerShell. [crayon-697eefb8a96df648076351/] [crayon-697eefb8a96e1238808495/] In the above code, a string variable named $text is initialized with the value of Hello, world! and another string variable called $prefix is created with the value of Hello. After that, using the StartsWith() method, the […]