This question already has answers here:
Expanding variables in file contents
(3 answers)
Powershell reevaluate string to array
(1 answer)
Closed 2 months ago.
Task: make PowerShell 5 to read/activate user input that contains any variable. This doesn't work on both script defined and environment variables. E.g.:
$H=4
$testParA=Read-Host "Try to input enviroment variable `$Env:windir :" #user input is $H
$testParB=Read-Host "Try to input variable `$H :" #user input is $H
Write-Host $testParA
Write-Output $testParA
Write-Host $testParB
Write-Output $testParB
Result:
> $Env:windir
> $Env:windir
> $H
> $H
Expectation:
> C:\Windows
> C:\Windows
> 4
> 4
Original task: In a text generator PowerShell script, let users to customize text, and where to place the serial counter variable "$serial". E.g., "some text $serial some other text"
No-duplication: This question has been marked as duplicate from "https://stackoverflow.com/questions/1667023/expanding-variables-in-file-contents", but not really. The answer is surely duplicated, but the question part is very different (You have to know "expand variables" to be able to find this question from searching, which is why I was unable to get answers before posting this question), and could have other methods to achieve the same or a better result. In general, on closing this question will only cause more people who never heared of "expand variables" to further create similar questions. There is probably a better method than simply closing this question.
Related
This question already has answers here:
Are these alternatives to Invoke-Expression really any safer? And why?
(1 answer)
Powershell reevaluate string to array
(1 answer)
Closed 4 months ago.
I have a variable that stores a command that I would like to execute.
ex:
$C = "echo 'test'"
I have one way to execute this by using the following:
powershell .($C)
however this opens a new instance of powershell. How can I go about executing that code in the same instance? I can't seem to find a way to use dot sourcing without opening that second instance.
EDIT: I can not use IEX
This question already has answers here:
Return value of environment variable with powershell
(2 answers)
Closed 4 years ago.
Using Powershell prompt, I cannot seem to echo a system environment variable that has a space in it, stored in a variable.
For instance, if I do a simple:
echo ${env:My Var}
I get back the value of that environment variable - which is the word "test". That works great.
Then, if I echo a preset variable that is set to the string "My Var", I get back what I'd expect, the word "My Var".
echo $variable
If I then echo a hybrid of the two:
echo env:${variable}
I also get back what I'd expect, which is the string value "env:My Var".
But now I want to use that $variable to output the value of the corresponding environment variable. Meaning, I want to use something like:
echo ${env:${variable}}
... and get back the value of the environment variable, the word "test".
But that doesn't work. I get a red error saying Use `{ instead of { in variable names. But nothing like that works either.
Any help would be appreciated.
this worked for me:
cd env:
new-item 'test 123' -Value "123"
${env:test 123}
123
$var = 'test 123'
(Get-item env:$var).value
123
(Get-item env:$var).name
Test 123
This question already has answers here:
Get only filename from full path of a file
(2 answers)
Closed 5 years ago.
I'm writing a PowerShell script to output the process,
$Process=chknull $_.Properties[0].Value
$Process_new=$Process -replace '(?<! .*) ','_'
The above code outputs the result as entire path of process, Eg:
C:\Windows\System32\Notepad.exe.
Is there a way, where I can get just "Notepad.exe"
If you're getting a full path return like that as a string you can set the value into a variable and then use split on it and grab the last entry in the resulting array.
Example:
("C:\Windows\System32\Notepad.exe." -split "\\")[-1]
Remember to use two "\" as it is also an escape character in regex.
([IO.FileInfo]"C:\Windows\System32\Notepad.exe").Name
This question already has an answer here:
Import Variables from Text File in Powershell
(1 answer)
Closed 6 years ago.
Lets say I have a text file full of variables.
I would like to create a separate file in which I can read and call these variables using powershell.
EX. Variable.txt contains $X=10
Main.ps1 retrieves the variable X from the text file, and doubles it.
Apologize is this is an amateur question, I am very new to powershell.
For your specific purpose, you could have a file, data.ps1 containing your data, like so:
$x=10
Then, a script.ps1 file, similar to the following:
. ./data.ps1
$x = $x * 2
Write-Host "Value X is $x"
Which would give you the following output when you run ./script.ps1:
Value X is 20
This question already has answers here:
What does the '`' character do in Perl?
(3 answers)
Closed 8 years ago.
I am a beginner in perl.
Just wanted to understand the following code.
sub get_files {
foreach my $customer (keys %customers){
lg("Getting files from ftp for customer $customer", "LOG");
my $ftp_server = $customers{$customer}{'FtpServer'};
my $ftp_user = $customers{$customer}{'FtpUser'};
my $ftp_pass = $customers{$customer}{'FtpPass'};
my $datadir = $datafiles.$customer."/";
`$get_files $ftp_server $ftp_user $ftp_pass $datadir`;
}
}
What does last line in the above subroutine tells?
It invokes the command that's in the string $get_files, passing the command the remaining strings as parameters.
Usually it's used if you want to capture the resulting output and store it in a variable. In this case where the result is being discarded it would be more usual to use system instead:
system $get_files, $ftp_server, $ftp_user, $ftp_pass, $datadir;
although if the command does then produce any output it'll appear on-screen instead of being absorbed by the back-ticks operator.