Error message installing Chocolatey in PowerShell - powershell

I'm trying to install Chocolatey to use with PowerShell.
The recommended way to install it is copy and paste the following line.
#powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('http://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
But I get the following error:
At line:1 char:13
+ #powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object ...
+ ~~~~~~~~~~
Unexpected token '-NoProfile' in expression or statement.
At line:1 char:24
+ #powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object ...
+ ~~~~~~~~~~~~~~~~
Unexpected token '-ExecutionPolicy' in expression or statement.
At line:1 char:150
+ ... nstall.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
+ ~~
The token '&&' is not a valid statement separator in this version.
At line:1 char:1
+ #powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object ...
+ ~~~~~~~~~~~
The splatting operator '#' cannot be used to reference variables in an expression. '#powershell' can be used only as
an argument to a command. To reference variables in an expression use '$powershell'.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
ExecutionPolicy is set as RemoteSigned and I'm running Powershell v3
I tried some apply some bits of the installation code rather than the whole line, but basically, anything after #Powershell is an unexpected token.

You must start that line from cmd.exe (a "standard" command prompt), not from PowerShell.

In PowerShell v3+ the easiest way is:
Open a PowerShell window (run as administrator)
Check the version of PowerShell is greater than 3:
$PSVersionTable.PSVersion
Enable execution of PowerShell scripts?
set-executionpolicy remotesigned
In PowerShell
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex

I was unable to install Chocolatey on my Windows 10 64-bit OS installation. I was getting powershell not recognized as internal or external command. Finally I found the solution, so to people who ever facing the exact same problem as I did, here is the solution for you.
The reason why you get such an error is because the WindowsPowerShell path is not set. So kindly set the Path as
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
Go to Environment variable (see below). You see that Path variable, click on Edit and you see one more pop-up window, which shows a couple of paths there. Now click on New and copy-paste the above path. Close your CommandPrompt(admin) and open it again. Run the command given by Chocolatey, and now it starts downloading.
Here is a step-by-step guide:
Go to Control Panel → System → Advanced System Settings → Environment Variables → User variable for Users → Select Path Variable → click Edit → Click on New → paste this %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\ → Click OK → you're done.

Related

Powershell Switch Parameter throws ParameterBindingException Exception

I have to execute the below script thru an Integration Tool Mulesoft. Unfortunately the switch parameter Confirm is throwing an error. The same script works from the powershell command line. Below is the command to execute:
Set-Location -Path 'C:\Windows\System32\WindowsPowerShell\v1.0\';powershell.exe -ExecutionPolicy RemoteSigned -noprofile -noninteractive ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Disable-MailUser -Identity abc.com/PEOPLE/xyzpqr -Confirm:$false"
Operation in flow:
<powershell:execute-command-exception-handling-enabled doc:name="Execute command (exception handling enabled)" doc:id="79bd2451-9441-49e7-8517-3bbd6a151fae" config-ref="Powershell_Configuration" command="Set-Location -Path 'C:\Windows\System32\WindowsPowerShell\v1.0\';powershell.exe -ExecutionPolicy RemoteSigned -noprofile -noninteractive ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Disable-MailUser -Identity abc.com/PEOPLE/xyzpqr -Confirm:$false""/>
The error is as below
Message : A positional parameter cannot be found that accepts argument '-Confirm:False'.
+ CategoryInfo : InvalidArgument: (:) [Disable-MailUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Disable-MailUser
Our PS version is 5.1 Any help would be greatly appreciated I tried
After trying a lot of suggestions in Stackoverflow([How to convert string to boolean in this Powershell code for Exchange Online?][1]) etc here is what worked
I did a cast
-Confirm:([System.Convert]::ToBoolean(0))
and that worked. Strangely
-Confirm:([System.Convert]::ToBoolean("False")) also didn't work.
So a cast was required. Mulesoft Powershell connector must have been playing tricks.
[1]: https://stackoverflow.com/questions/16983810/how-to-convert-string-to-boolean-in-this-powershell-code-for-exchange-online
Assuming your local machine has a Windows OS , having the code working in your local makes sense. But It won't work in Cloudhub because it uses an EC2 instance under the hood with a Linux OS.
You can find more details about it in https://docs.mulesoft.com/runtime-manager/cloudhub-architecture#cloudhub-workers

Why is the PyCharm Terminal showing "activate.ps1 cannot be loaded because running scripts is disabled on this system"? [duplicate]

This question already has answers here:
PowerShell says "execution of scripts is disabled on this system."
(48 answers)
Closed 8 months ago.
When I try to run ionic commands like ionic serve on the VS Code terminal, it gives the following error.
How can I fix this?
ionic : File C:\Users\Lakshan\AppData\Roaming\npm\ionic.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ ~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
I found a way to fix this error.
It is as follows:
First, Open PowerShell with Run as Administrator.
Then, run this command in PowerShell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
After that type Y and press Enter.
This is because of Execution Policy. This defines how powershell scripts will run.
In Default windows desktops, it is Restricted, not allowing any scripts (signed or unsigned) only interactive sessions.
So best is you set using RemoteSigned (Default on Windows Server) letting only signed scripts from remote and unsigned in local to run, but Unrestriced is insecure lettting all scripts to run.
To set run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned as administrator.
This code will fix it:
Set-ExecutionPolicy RemoteSigned –Scope Process
To Bypass this you need to change the Execution policy.
Type this into your terminal.
Set-ExecutionPolicy -Scope CurrentUser
then it will prompt you to supply a value at which point you can set Bypass / RemoteSigned or Restricted.
cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: "RemoteSigned" or "Bypass" or "Restricted".

How to fix "running scripts is disabled on this system"? [duplicate]

This question already has answers here:
PowerShell says "execution of scripts is disabled on this system."
(48 answers)
Closed 8 months ago.
When I try to run ionic commands like ionic serve on the VS Code terminal, it gives the following error.
How can I fix this?
ionic : File C:\Users\Lakshan\AppData\Roaming\npm\ionic.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ ~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
I found a way to fix this error.
It is as follows:
First, Open PowerShell with Run as Administrator.
Then, run this command in PowerShell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
After that type Y and press Enter.
This is because of Execution Policy. This defines how powershell scripts will run.
In Default windows desktops, it is Restricted, not allowing any scripts (signed or unsigned) only interactive sessions.
So best is you set using RemoteSigned (Default on Windows Server) letting only signed scripts from remote and unsigned in local to run, but Unrestriced is insecure lettting all scripts to run.
To set run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned as administrator.
This code will fix it:
Set-ExecutionPolicy RemoteSigned –Scope Process
To Bypass this you need to change the Execution policy.
Type this into your terminal.
Set-ExecutionPolicy -Scope CurrentUser
then it will prompt you to supply a value at which point you can set Bypass / RemoteSigned or Restricted.
cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: "RemoteSigned" or "Bypass" or "Restricted".

Powershell doesn't running scripts even after setting execution policy

I am trying to run virtualenv script to activate it on powershell using:
.\env\Scripts\activate.ps1
but I get this error:
.\env\Scripts\activate.ps1 : File C:\Users\user\Desktop\tranning\env\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\env\Scripts\activate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
I read this article and I tried to solve this by running powershell as administrator and type this command:
Set-ExecutionPolicy RemoteSigned
but unfortunately nothing change
According to this earlier asked question there are a few other things you can try:
PowerShell says "execution of scripts is disabled on this system."
To summarize from the link:
If you are running from a windows server make sure you set the execution policy on all versions of powershell that you have on the system(both x64 x86 versions of powershell)
OR
Run the following to bypass the Execution Policy:
powershell -ExecutionPolicy ByPass -File script.ps1

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)