• PowerShell remove special characters from String
    14 July

    PowerShell 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 […]

  • PowerShell trim string after character
    29 June

    PowerShell 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 […]

  • PowerShell Split String by multiple delimiters
    07 June

    PowerShell 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, […]

  • PowerShell Concatenate String and variable
    28 May

    Concatenate 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 […]

  • PowerShell add quotes to String
    27 May

    PowerShell 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 […]

  • PowerShell check if String contains List
    25 April

    PowerShell 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 […]

  • PowerShell Split and get last value
    24 April

    PowerShell 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 […]

  • Run String as Command in PowerShell
    22 April

    Run 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 […]

  • PowerShell check if String starts with
    22 April

    PowerShell 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, […]

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.