Replace Space with Underscore in PowerShell

💡 TL;DR

To replace space with underscore in PowerShell, use String’s replace() method or PowerShell’s replace operator.

Here is simple example:

There are multiple ways to replace Space with Underscore in PowerShell. Let’s go through them.

Using String’s replace() method

You can use string’s replace() method to replace space with underscore in PowerShell.

Replace method returns new string and replaces each matching text with new text.
Replace methods takes two arguments:

  • substring to find in given string : space
  • replacement String for found String: underscore

Using replace operator

You can use PowerShell’s replace operator to replace space with underscore in PowerShell.

Replace operator takes two arguments:

  • substring to find in given string : space
  • replacement String for found String: underscore

As you can see, replace operator replaced all spaces with underscore in the String.

Please note that we have assigned result of replace() method or replace() operator to new string,

Was this post helpful?

Related Posts

  • Array of HashTables in PowerShell
    02 February

    PowerShell – Array of HashTables

    Table of ContentsCreating an Array of HashTablesPassing Data from a Parameter to an Array of HashTable Creating an Array of HashTables To create an array of hashtables in PowerShell: Create an array. Create two or more hash tables. Use += operator to add the hash tables (created in the second step) to an array (created […]

  • Write Binary Files in PowerShell
    01 February

    Write Binary Files in PowerShell

    Table of ContentsUsing Add-Content CmdletUsing the [IO.File]::WriteAllBytes() MethodUsing FromBase64CharArray() with Add-Content Using Add-Content Cmdlet Use the Add-Content cmdlet to write binary files in PowerShell. [crayon-63db04707e1c7976094334/] This script created a new file named test.bin in the current working directory and wrote the binary data to the file. The script will overwrite the existing file with the […]

  • 01 February

    What Does Percent(%) Mean in PowerShell

    Table of ContentsUsing Percent (%) in Different ContextsUse Percent(%) as an alias of the ForEach-Object cmdlet in PowerShellUse percent (%) as the Modulus Operator in PowerShellUse percent(%) as an assignment operator (%=) in PowerShell Using Percent (%) in Different Contexts Typically the % character is for mod functionality. But in PowerShell, it’s an alias for […]

  • 28 January

    PowerShell – Get Number of Lines in CSV File

    Table of ContentsUsing Get-Content CmdletUsing Import-Csv CmdletUsing Get-ChildItem with Foreach-Object Using Get-Content Cmdlet The Get-Content cmdlet can be used in the following ways to get the number of lines in the specified .csv file: Get-Content cmdlet with .Length property Get-Content cmdlet with .Count property Get-Content cmdlet with .ReadCount property Get-Content cmdlet with Measure-Object cmdlet and […]

  • Read CSV file in PowerShell
    28 January

    Read CSV File in PowerShell

    Table of ContentsUsing the Import-Csv CmdletUsing the ConvertFrom-Csv CmdletUse the ConvertFrom-Csv Cmdlet with the Pipe (|) OperatorUse the ConvertFrom-Csv Cmdlet with the -InputObject Parameter Using the Import-Csv Cmdlet To read a CSV file in PowerShell, use the Import-Csv cmdlet. [crayon-63db04707e5a8936932494/] [crayon-63db04707e5ab324441950/] PowerShell provides several cmdlets that allow us to work with CSV files: Import-Csv ConvertFrom-Csv […]

  • Read File into String in PowerShell
    27 January

    Read File into String in PowerShell

    Table of ContentsUsing the Get-Content CmdletUse the Get-Content Cmdlet to Read a Single FileUse the Get-Content Cmdlet with forreach loop to Read Multiple FilesUsing [System.IO.File]::ReadAllText() MethodUse the [System.IO.File]::ReadAllText() Method with foreach loop to Read Multiple FilesUsing the Variable Notation with the Out-String Cmdlet Using the Get-Content Cmdlet We can use the Get-Content cmdlet to read […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.