- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to format string using PowerShell?
To format a string in a PowerShell we can use various methods. First using the simple expanding string method.
PS C:\> $str = 'PowerShell' PS C:\> Write-Output "Hello $str !!!!" Hello PowerShell !!!!
Second, using the format method. In this method, we will use the Format function of the String .NET class.
PS C:\> $str = "PowerShell" PS C:\> [String]::Format("Hello $str...!!!") Hello PowerShell...!!!
The third method using the Format operator. We can use the number format here as shown below.
PS C:\> $str = 'PowerShell' PS C:\> "Hello {0}" -f $str Hello PowerShell
If we have multiple variables then we need to increase the numbers inside the curly brackets. For example,
PS C:\> $str = "PowerShell" PS C:\> $str1 = "Azure" PS C:\> "Hello {0} and {1}" -f $str,$str1 Hello PowerShell and Azure
You can also use the same format operator inside the format method.
PS C:\> [String]::Format("Hello {0} and {1}", $str,$str1) Hello PowerShell and Azure
Advertisements