Execute a bat file with a variable name in the path - powershell

I am having a problem executing a powershell script where I want to point to different versions of a bat file based on some arguments
Hard coding the path as follows works with no issues:
& 'C:\Program Files\MATLAB\R2022a\bin\mcc.bat' -W $command -T link:exe ...
but replacing a folder name with a variable causes it to fail:
$Matlab_version = "R2022a"
& 'C:\Program Files\MATLAB\$Matlab_version\bin\mcc.bat' -W $command -T link:exe
with the following output:
& : The term 'C:\Program Files\MATLAB\%$Matlab_version%\bin\mcc.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ & 'C:\Program Files\MATLAB\%$Matlab_version%\bin\mcc.bat' -W $comman ...
I've tried even creating a variable with the path name
$mcc_loc = & 'C:\Program Files\MATLAB\$Matlab_version\bin\mcc.bat'
Write-Host $mcc_loc
which produces a string with the correct path. Any help is greatly appreciated. My dumb solution is to have an if statement for each $Matlab_version and hard code it in there but I'm sure there has to be a more elegant solution

It's because you are using single quotation marks.
variables will not be interpreted within single marks, everything will be taken as a literal string.
use double quotes instead when passing in variables mid string.
example:

You need to change the single quote ' to double quote "
Double-quoted strings
A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.
$Matlab_version = "R2022a"
& "C:\Program Files\MATLAB\$Matlab_version\bin\mcc.bat" -W $command -T link:exe
See about_Quoting_Rules

Related

How do I invoke a command using a variable in powershell?

I want to invoke the shutdown.exe executable in powershell and it is located on C:\WINDOWS\System32\shutdown.exe.
Since we already got the C:\WINDOWS in the variable $env:windir I want to just use it and concatenate the rest of the path into the command. I was trying this:
PS C:\Users\shina> .\$env:windir\System32\shutdown.exe -s
But I got the following error:
.\$env:windir\System32\shutdown.exe: The term '.\$env:windir\System32\shutdown.exe' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
How can I accomplish this?
You can use:
& $env:windir\System32\shutdown.exe -s
Or
. $env:windir\System32\shutdown.exe -s
(note the space).
& is the call operator. You can read more about it here:
Runs a command, script, or script block. The call operator, also known as the "invocation operator", lets you run commands that are stored in variables and represented by strings or script blocks.
. is the dot sourcing operator. You can read more about it here:
Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope, overriding existing ones.
The dot sourcing operator is followed by a space. Use the space to distinguish the dot from the dot (.) symbol that represents the current directory.

PowerShell command only works without spaces between arguments

I try certain source codes using PowerShell to extract an password protected archive using 7zip:
This command doesn' work (7zip is an alias for $7zipPath):
& 7zip x "$zipFile" -o "$output" -p $zipFilePassword
I get the this error:
Command Line Error:
Too short switch:
But when I remove the spaces between the variables -o and -p, the archive can be extracted. This behaviour confuses me with other command line tools like git etc.? Why is it so?
The behavior is specific to 7-Zip (7z.exe) and applies to whatever program (shell) you invoke it from:
Deviating from widely used conventions observed by CLIs such as git, 7z requires that even switches (options) that have mandatory arguments, such as -o and -p, have the argument directly attached to the switch name - no spaces are allowed:
& 7zip x $zipFile -o"$output" -p"$zipFilePassword"
Note that you normally need not enclose variable references in PowerShell in "..." (note how $zipFile isn't), even if they contain spaces. However, in order to attach them directly to switch names, you do.
Alternatively, you could enclose the entire token - switch name and argument - in double quotes:
& 7zip x $zipFile "-o$output" "-p$zipFilePassword"

Sending a pipe as an argument to an external program in Powershell?

I keep getting the following error when running a command to look for the most recently modified directory in my path: remotely from PowerShell:
head: The term 'head' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line 1: char: 95
+ ... d /somedir ; (lastmod=$(ls -tda -- */ | head -n 1); ....
I'm running code something along the lines of the following:
& plink "bob#server" "cd /somedir ; (lastmod=$(ls -tda -- */ | head -n 1); ls -la $lastmod)"
You've encapsulated your commands in double-quotes so the $ signs are being expanded by PowerShell. If you use single quotes this won't happen.
Just to add on to Bruce's answer, you could also escape the special character from your double-quoted string using the grave accent (`), which is the escape character in powershell.
& plink "bob#server" "cd /somedir ; (lastmod=$(ls -tda -- */ `| head -n 1); ls -la $lastmod)"
This means that if you did need any variables inside of your string, you could still utilize special characters.

How can I execute an external program with parameters in PowerShell?

I have read this answer stackoverflow answer and it get's me there half way. Here is what I need to do.
Execute this command:
"c:\myexe.exe <c:\Users\Me\myanswerfile.txt"
If I run that straight from within my powershell script
&'c:\myexe.exe <c:\Users\Me\myanswerfile.txt'
I get this error:
The term 'C:\myexe.exe <c:\Users\Me\myanswerfile.txt' is not recognized as the name of
a cmdlet, function, script file, or operable program. Check the spelling of the name,or
if a path was included, verif that the path is correct and try again.
Now I have tried several variations of this including placing the original command in a variable called $cmd and then passing the
If I append the '<' to the $cmd variable the command fails with a similar error as the first one.
I'm stumped. Any suggestions?
If you want to run a program, just type its name and parameters:
notepad.exe C:\devmy\hi.txt
If you want to run an exe and redirect stdin to it which your example seems to be an attempt of, use:
Get-Content c:devmy\hi.txt | yourexe.exe
If you need to specify the full path to the program then you need to use ampersand and quotes otherwise powershell thinks you are defining a plain string:
&"C:\Program Files (x86)\Notepad++\notepad++.exe"
Simply use & operator
& "Program\path\program.exe" "arg1" "arg2" ....

How to pass a variable as an argument to a command with quotes in powershell

My powershell script takes the following parameter:
Param($BackedUpFilePath)
The value that is getting passed into my script is:
"\123.123.123.123\Backups\Website.7z"
I have another variable which is the location I want to extract the file:
$WebsiteDeploymentFolder = "C:\example"
I am trying to extract the archive with the following command:
`7z x $BackedUpFilePath -o$WebsiteDeploymentFolder -aoa
I keep getting the following error:
Error:
cannot find archive
The following works but I need $BackedUpFilePath to be dynamic:
`7z x '\123.123.123.123\Backups\Website.7z' -o$WebsiteDeploymentFolder -aoa
I think I need to pass $BackedUpFilePath to 7z with quotes but they seem to get stripped out no matter what I try. I am in quote hell.
Thanks.
EDIT: It turns out the problem was I was passing in "'\123.123.123.123\Backups\Website.7z'". (extra single quotes)
The easiest way to work with external command line applications in PowerShell (in my opinion) is to use aliases. For example, the following works fine for me.
Set-Alias Szip C:\Utilities\7zip\7za.exe
$Archive = 'C:\Temp\New Folder\archive.7z'
$Filename = 'C:\Temp\New Folder\file.txt'
SZip a $Archive $Filename
PowerShell takes care of delimiting the parameters correctly.