When I run the below powershell command from powershell to get the app version, I get the correct response
(Get-Item "C:\\Program Files\\CompanyName\\AppName\\bin\\AppLauncher.exe").VersionInfo.FileVersion
However I need to run it from batch file so I add the following in my batch file.
powershell -Command " & (Get-Item 'C:\\Program Files\\CompanyName\\AppName\\bin\\AppLauncher.exe').VersionInfo.FileVersion"
On executing this, I see the version being returned on the console but with exception
& : The term '1.2.0.33' 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:4
+ & (Get-Item 'C:\\Program Files\\CompanyName\\AppName ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (1.2.0.33:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
'1.2.0.33' in the above error is the version I am expecting it to return.
Also if it could be suggested as to how to get this assigned to a variable.
You should be able to use either:
#%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"%ProgramFiles%\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"
Or:
#%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"$Env:ProgramFiles\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"
If you want to save it as a variable then perhaps:
#For /F %%G In ('%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"%ProgramFiles%\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"') Do #Set VerInfo=%%G
or:
#For /F %%G In ('%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"$Env:ProgramFiles\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"') Do #Set VerInfo=%%G
Related
Following guides to create an alias in powershell, I have run the following command:
Set-Alias rush-enable "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass"
This sets the alias. However when I run the alias I hit an error.
PS C:\example> rush-enable
rush-enable : The term 'Set-ExecutionPolicy -Scope Process
-ExecutionPolicy Unrestricted' 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:1
+ rush-enable
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-ExecutionPo.
..cy Unrestricted:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
However running the command on it's own works fine:
PS C:\example> Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted
PS C:\example>
Am I missing something? How should I format the command to work as an alias?
As per the documentation:
You can assign an alias to a cmdlet, script, function, or executable
file.
You cannot assign an alias to a command and its parameters. For
example, you can assign an alias to the Get-Eventlog cmdlet, but you
cannot assign an alias to the Get-Eventlog -LogName System command.
This is presumably a way to avoid having to disambiguate a parameter that exists on an alias and which is passed when invoking the alias.
You therefore have two options:
Encapsulate your command in a function called Enable-Rush (note the PowerShell acceptable naming scheme, if you care). In your case this would look something like:
function Enable-Rush {
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
}
Save the command as a file and create a alias to the file.
And to persist it, put it in a $PROFILE. Depending on your system, you might need to digitally sign the $PROFILE or change the PowerShell system Execution Policy to allow PowerShell scripts generated by the system to execute when starting a PowerShell process.
Background
I have a batch file that is running a PowerShell Script. I've added this batch file to the startup folder of approximately 60 windows computers which are running either the W7 or W10 OS. It runs successfully on 95% of the PCs. On the remaining 5%, the batch file doesn't run and the error that's outputted is shown below.
The string is missing the terminator '.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Code
This is the code I've written on the batch file.
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%UserProfile%\Desktop\Test.ps1""' -Verb RunAs}"
Question
What can I do to fix this error and have the batch file run successfully on all computers?
I wish to run the following command from within Powershell 5 but make it execute with Powershell 6:
$PSVersionTable.PSVersion.Major
How can this be done?
The expected return value from the command should be '6'.
I am getting this:
pwsh -command "$PSVersionTable.PSVersion.Major"
System.Collections.Hashtable.PSVersion.Major : The term 'System.Collections.Hashtable.PSVersion.Major' 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:1
+ System.Collections.Hashtable.PSVersion.Major
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (System.Collections.\u2026ble.PSVersion.Major:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The problem is the double-quotes. PowerShell 5 is using string interpolation on the string before passing it to pwsh. So, what you see as:
pwsh -command "$PSVersionTable.PSVersion.Major"
Is actually passed as:
pwsh -command "System.Collections.Hashtable.PSVersion.Major.PSVersion.Major"
The solution is to use single quotes (which 'switches off' interpolation):
pwsh -command '$PSVersionTable.PSVersion.Major'
I tried the solution given from: Specify the size of command prompt when executing a batch file
I ran:
powershell -command "&{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But I get these errors, any ideas?
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. At line:1 char:22 + &{set-executionpolicy <<<< remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Import-Module : The specified module 'SetConsoleFont' was not loaded because no valid module file was found in any module directory . At line:1 char:50 + &{set-executionpolicy remotesigned; Import-Module <<<< SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : ResourceUnavailable: (SetConsoleFont:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
The term 'Get-ConsoleFontInfo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spe lling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:86 + &{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo <<<< | Format-Table -AutoSize} + CategoryInfo : ObjectNotFound: (Get-ConsoleFontInfo:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
I have put the file SetConsoleFont.psm1 in
C:\Users\Adrian\Documents\WindowsPowerShell\Modules\SetConsoleFont
You say "You're not allowed to set the execution policy" well maybe I'm not, but it's my machine so why shouldn't I? I don't want to execute these commands as Administrator, just as a user, me (Adrian)
Another comment was to try set-executionpolicy bypass process
so I tried:
powershell -command "&{set-executionpolicy bypass process; set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But got even more red errors.
I have no idea what powershell is or how to use it, I just want to change the font from a batch file without hassle!
Try set-executionpolicy bypass process instead.
Also make sure you have put the module in a module path folder such as:
[yourprofile]\Documents\WindowsPowershell\Modules
I managed to get it working but only in a PowerShell console, and I had to run it as Administrator. However this is not practical for me for the following reasons:
I wish to change the font of new window seamlessly from a batch file, which will be run by users of the software. They may not have Administrator access and so cannot execute "set-executionpolicy remotesigned" which I needed to do to get it working.
Also this has to be done in a DOS batch file, so opening up a powershell window is not an option. It only works in a PowerShell window and not with the DOS "powershell -command" option.
So a partial answer.
If you want to change Execution Policy, it should be done in an elevated prompt.
And loading the module can be done by giving absolute path. Example is below.
Import-Module c:\users\testuser\desktop\SetConsoleFont.psm1 -Verbose
and we can bypass execution policy like below.
powershell.exe -executionpolicy bypass -command "${<your code>}"
Edit: The imported module will be available only in the scope of the script block.
here it is with in {}. So whatever cmdlets and functions in side the module should be executed in sided the scriptblock.
Regards,
Kvprasoon
I tried running one of the .bat files with CMD.
The first line it works
powershell -Command "Set-ExecutionPolicy Unrestricted"
powershell -Command "& {.\SetupLabxx.ps1}" -NoExit
pause
The second line gives me this exception
The term '.\SetupLabxx.ps1' is not recognized as the name of a cmdlet,
function , script file, or operable program. Check the spelling of the
name, or if a pat h was included, verify that the path is correct and
try again. At line:1 char:20 + & {.\SetupLabxx.ps1 <<<< } -NoExit +
CategoryInfo : ObjectNotFound: (.\SetupLabxx.ps1:String) [], Co
mmandNotFoundException + FullyQualifiedErrorId :
CommandNotFoundException
You can combine the commands (use full path to script):
powershell.exe -NoExit -ExecutionPolicy Unrestricted -File c:\SetupLabxx.ps1