why can't I create a here-string in Powershell ISE? - powershell

Trying to create a here-string
> $scriptblock =#'
The string is missing the terminator: '#.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Why am I getting this in the Powershell ISE, but it works normally in Powershell?
Version 5.1.
> $scriptblock =#'
>>
No, there's no space after the single-quote. I just hit enter.
I even closed and reopened ISE and same thing.

Answer provided by Mathias R. Jessen:
Press [Shift+Enter] to insert a literal newline in the ISE console

Related

Environment opens in CMD but not powershell

Amateur computer science student here.
Trying to activate a virtual environment in powershell and I keep
getting the following error both when I start powershell or try to
activate the environment. The environment opens normally in CMD.
I've tried reinstalling to no avail. Not sure what to do so making a first post on StackOverflow.
Many thanks and kind regards,
Daniel
PS C:\Users\User> conda activate MyDjangoEnv
Invoke-Expression : At line:1 char:1342
... iles\PuTTY;"C:\Users\User\AppData\Local\Packages\PythonSoftwareFounda ...
Unexpected token 'C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\loc
al-packages\Python39\Scripts' in expression or statement.
At C:\Users\User\anaconda3\shell\condabin\Conda.psm1:76 char:9
+ Invoke-Expression -Command $activateCommand;
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand

How to execute command in Powershell 6 (core) from within Powershell 5?

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'

Powershell inside AutohotKey is not working

I can run any PowerShell command inside AHK file. For example, this command below:
Run, PowerShell.exe -noexit -Command "Start notepad; start calc.exe",, hide
I have Powershell command that I can run it from PowerShell fine and its working great which will add user as local admin on remote machine:
([ADSI]"WinNT://serverabc123/Administrators,Group").Add("WinNT://HMM.org.br/Paul1")
However, when I use the above code on AHK like it's not working!
Run, PowerShell.exe -noexit -Command "([ADSI]"WinNT://serverabc123/Administrators,Group").Add("WinNT://HMM.org.br/Paul1")",, hide
I keep getting this error:
At line:1 char:8<>
+ ([ADSI]WinNT://serverabc123/Administrators
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'WinNT://serverabc123/Administrators' in expression or statement.
At line:1 char:8
+ ([ADSI]WinNT://serverabc123/Administrators
+ ~
Missing closing ')' in expression.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Can anyone advice on what to do? I could write external file PS1 and call it from the AHK then delete it, but I want to have it running from my single AHK file.
Try the following:
Run, powershell.exe -noexit -command ([ADSI]\"WinNT://serverabc123/Administrators`,Group\").Add(\"WinNT://HMM.org.br/Paul1\")
That is:
Don't use outer quoting.
\-escape " characters (required by PowerShell)
`-escape , characters (required by AutoHotkey)

How to start powershell in eclipse

I try to start powershell as an external tool.
I want it to be run in the console windows of eclipse.
But it starts with empty console, and when I type and press return, I get:
Missing expression after unary opearator ‘-’.
At line:1 char:2
+ – <<<< Command Set-Location C:\Users\XXX
+ CategoryInfo : ParserError: (-:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
You got misplaced quotes in the Arguments section.
-NoExit -"Command Set-Location ${project_loc}"
Try moving the quote after Command switch like so,
-NoExit -Command "Set-Location ${project_loc}"

Executing an EXE in your $PATH, which starts with a numeric value, in Powershell?

So 7z.exe is in my $PATH environment variable.
PS C:\Users\jimmeh> 7z.exe
Bad numeric constant: 7.
At line:1 char:2
+ 7 <<<< z.exe
+ CategoryInfo : ParserError: (7:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : BadNumericConstant
Besides renaming the EXE, is there a way to do this, or is it a bug in Powershell?
It work like
& '.\7z.exe'
if you want to specify an archive to extract
& '.\7z.exe' e your.zip
This is no longer a problem with Powershell 4.0
http://www.microsoft.com/en-us/download/details.aspx?id=40855