Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I append an underscore to a string variable in PowerShell without it interpreting a carriage return in a .ps1 file?

Tags:

powershell

How can I append a - or _ to a string variable in PowerShell it interpreting a carriage return inside a script file?

I would like the _* appended to the end of my $curUserID variable, so that I can then use this as a filter.

I have tried lots of ways to get this, but all of them result in a carriage return after my curUserID value when inside of a script file. It works fine when I do the same at a command line in PowerShell?!

Here is my code:

$curUserID = "28";
$filePrefix = "${curUserID}$('_*')";

when I run this from inside of a script file, I get the following

28
_*

But what I need is the following, which is what I get when I run the above at the command line.

28_*
like image 624
user2528302 Avatar asked Jun 27 '13 13:06

user2528302


People also ask

How do you append text to a variable in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."

How do you pass special characters in PowerShell?

The escape character can be used in three ways: 1) When used at the end of a line, it is a continuation character - so the command will continue on the next line. 3) When used inside double quotation marks, the escape character indicates that the following character should be interpreted as a 'special' character.

What does $_ mean in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


1 Answers

$filePrefix = "${curUserID}_*"
like image 131
Shay Levy Avatar answered Sep 30 '22 03:09

Shay Levy