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_*
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."
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.
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.
$filePrefix = "${curUserID}_*"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With