PowerShell: How to Use Write-Host and Display Tab Character


You can use the Write-Host cmdlet in PowerShell to output text to the console.

To display a tab character when using Write-Host, you can use the following methods:

Method 1: Use `t

Write-Host "This`t string contains a tab"

Method 2: Use [char]9

$tab_char = [char]9

Write-Host "This$tab_char string contains a tab" 

Both of these methods produce the same result.

The following examples show how to use each method in practice.

Example 1: Use `t to Display Tab in PowerShell

Suppose that you would like to use the Write-Host cmdlet in PowerShell to display the following text:

  • This    string contains a tab

We can use the following syntax to do so:

Write-Host "This`t string contains a tab"

The following screenshot shows how to use this syntax in practice:

PowerShell Write-Host with tab character

Notice that the output displays the text that we specified with a tab in the location where we used `t in the string.

Example 2: Use [char]9 to Display Tab in PowerShell

Once again, suppose that you would like to use the Write-Host cmdlet in PowerShell to display the following text:

  • This    string contains a tab

Another way to do so is by using the [char]9 command:

$tab_char = [char]9

Write-Host "This$tab_char string contains a tab"

The following screenshot shows how to use this syntax in practice:

Notice that the output displays the text that we specified with a tab in the location where we used [char]9 in the string.

Also notice that this produces the same result as the previous example. Feel free to use whichever method you prefer.

Note: You can find the complete documentation for the Write-Host cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Write-Host with Specific Colors
PowerShell: How to Replace Special Characters in String
PowerShell: How to Replace Text in String

Leave a Reply