What is the equivalent for vbScript function string() in powershell ?

Peter Bødker 0 Reputation points
2023-04-15T08:55:27.3033333+00:00

Hi I am having issues translating a vbScript to powershell. The purpose is to generate a line of chars to improve the readability of the script output. This is done in vbScript by use of the function string(), where I can generate a string of similar chars i.e. wscript.Echo String(20,"=") gives me: "====================" Can powershell generate something like this in a simple way? Br Peter

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-04-15T11:37:37.0566667+00:00

    You can just do this.

    "=" * 20
    

  2. Rich Matheisen 47,901 Reputation points
    2023-04-16T15:46:14.8+00:00

    If you're looking for a more direct replacement for that:

    [string]::new("=",20)
    
    

    But the answer from @MotoX80 is more intuitive (once you get past the "how does the string class perform multiplication?" puzzle!).

    Depending on your proclivity for diversity, you can also use "".PadRight("=",20) or "".PadLeft("=",20). :-)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.