I'm trying to set up conda for PowerShell but keep running into a strange error. I'm using miniconda3 (installed for all users) and PS version 7.3.1.
The conda executable is added to $Env:PATH and I'm able to run conda init powershell, which creates a profile.ps1 file in my PowerShell directory with the following contents:
#region conda initialize
Contents within this block are managed by 'conda init' !!
If (Test-Path "C:\ProgramData\miniconda3\Scripts\conda.exe") {
(& "C:\ProgramData\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion
However, when restarting the terminal, I get this error:
$Env:CONDA_EXE: The term '$Env:CONDA_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.
InvalidOperation: The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name,
a script block, or a CommandInfo object.
Invoke-Expression: Cannot bind argument to parameter 'Command' because it is null.
Notice the strange character in the beginning. I've seen a few discussions that have led me to believe that this might have something to do with the encoding of the profile.ps1 file or perhaps even the encoding of the output of conda.exe (if that makes any sense). I've tried changing the encoding of the profile file to UTF-8 (without BOM) and ANSI, as suggested in other threads (here and here), but this has not solved the problem.
Another thing that might be relevant to this is that when I run conda init powershell, the profile.ps1 file is actually created in a directory that's not the same as my "actual" user directory. This is because my Windows user name contains an accented character, so instead of C:\Users\AndrásAponyi\Documents\WindowsPowerShell\profile.ps1, the file gets written to C:\Users\Andr�sAponyi\Documents\WindowsPowerShell\profile.ps1 and I have to manually copy it to the correct location.
The same conda installation works perfectly fine with PowerShell version 5.1.
It seems that the issue is with the output of conda.exe that contains an unexpected character. This is causing the PowerShell profile to fail. There are a few things that you can try to resolve this issue:
Remove the existing profile.ps1 file
Create a new user profile without accented characters in the name or
rename the existing user profile to remove the accented characters
Re-run conda init powershell
Start a new PowerShell session and check if the issue has been
resolved
If the problem persists, you can try uninstalling and reinstalling Miniconda.
Related
I'm trying to create a powershell module to store some reusable utility functions. I created script module PsUtils.psm1 and script module manifest PsUtils.psd1 (used these docs). My problem is that when I import this module in another script Visual Code does not suggest parameters names. Here's a screenshot:
When I hover cursor over the function I only get this:
PsUtils.psm1
function Get-Filelist {
Param(
[Parameter(Mandatory=$true)]
[string[]]
$DirectoryPath
)
Write-Host "DIR PATH: $DirectoryPath"
}
PsUtils.psd1 (excerpt)
...
FunctionsToExport = '*'
I have Powershell extension installed. Do I need to install anything else to make the suggestions work? What am I missing?
Generally speaking, only auto-loading modules - i.e., those in one of the directories listed in environment variable $env:PSModulePath - are automatically discovered.
As of version v2022.7.2 of the PowerShell extension, the underlying PowerShell editor services make no attempt to infer from the current source-code file what modules in nonstandard directories are being imported via source code in that file, whether via Import-Module or using module
Doing so would be the prerequisite for discovering the commands exported by the modules being imported.
Doing so robustly sounds virtually impossible to do with the static analysis that the editor services are limited to performing, although it could work in simple cases; if I were to guess, such a feature request wouldn't be entertained, but you can always ask.
Workarounds:
Once you have imported a given module from a nonstandard location into the current session - either manually via the PIC (PowerShell Integrated Console) or by running your script (assuming the Import-Module call succeeds), the editor will provide IntelliSense for its exported commands from that point on, so your options are (use one of them):
Run your script in the debugger at least once before you start editing. You can place a breakpoint right after the Import-Module call and abort the run afterwards - the only prerequisite is that the file must be syntactically valid.
Run your Import-Module command manually in the PIC, replacing $PSScriptRoot with your script file's directory path.
Note: It is tempting to place the cursor on the Import-Module line in the script in order to use F8 to run just this statement, but, as of v2022.7.2, this won't work in your case, because $PSScriptRoot is only valid in the context of running an entire script.
GitHub issue #633 suggests adding special support for $PSScriptRoot; while the proposal has been green-lighted, no one has stepped up to implement it since.
(Temporarily) modify the $env:PSModulePath variable to include the path of your script file's directory.
The most convenient way to do that is via the $PROFILE file that is specific to the PowerShell extension, which you can open for editing with psedit $PROFILE from the PIC.
Note: Make sure that profile loading is enabled in the PowerShell extension's settings.
E.g., if your directory path is /path/to/my/module, add the following:
$env:PSModulePath+="$([IO.Path]::PathSeparator)/path/to/my/module"
The caveat is that all scripts / code that is run in the PIC will see this updated $env:PSModulePath value, so at least hypothetically other code could end up mistakenly importing your module instead of one expected to be in the standard locations.
Note that GitHub issue #880 is an (old) proposal to allow specifying $env:PSModulePath entries as part of the PowerShell extension settings instead.
On a somewhat related note:
Even when a module is auto-discovered / has been imported, IntelliSense only covers its exported commands, whereas while you're developing that module you'd also like to see its private commands. Overcoming this limitation is the subject of GitHub issue #104.
One of our users needs to invoke a PowerShell script from a shortcut, as using the command line is probably beyond their abilities. The path of the script contains spaces, and I have been unable to successfully get the script to run from the shortcut. The target of the shortcut is:
powershell.exe -file "C:\Users\xxxxx\xxxx xxxx\Finance - Documents\Secure\xxx (Finance Officer)\Rent\Data File Loader - XXXXX\InvokeDataLoader.ps1"
(I have replaced names of people and the company with x's)
Clicking on the shortcut just briefly opens a cmd window (I think) so I have have been trying to diagnose the problem by running the command in cmd.exe. However when I try to do that I get an error message that it cannot find the path "C:\Users\xxxxx\xxxx" i.e. it is splitting the path on the first space. Enclosing the path with single or double quotes does not change this, and if I try to escape the spaces with backticks I get a message that the path does not exist.
You actually don't need to change the PowerShell script itself to do this. When making the shortcut, simply set the target to:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "C:\Users\xxxxx\xxxx xxxx\Finance - Documents\Secure\xxx (Finance Officer)\Rent\Data File Loader - XXXXX\InvokeDataLoader.ps1"
I am trying to unzip a file on server 2012r2 through chef. I get an error back "illegal characters in path". When I manually type the commands in the file is extracted exactly as it should, but doing it through chef it just does not want to work for some reason.
powershell_script 'test' do
code <<-EOH [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
[System.IO.Compression.ZipFile]::ExtractToDirectory('c:\tmp\hp401n.zip', 'c:\tmp')
EOH
end
You need to escape your backslashes because they are getting processed by Ruby too, c:\\tmp\\hp401n.zip and C:\\tmp. Otherwise you probably have literal tab characters (\t) in the path.
I can't compile any Java programs after PowerShell replaced the Command Prompt in the file menu during a Windows update. I tried using the same SET PATH command that I used with the Command Prompt (set path = c:\"Program Files"\Java\jdk1.8.0_102\bin). The java command worked, but javac was not recognized.
After learning how to access the Command Prompt (type "CMD" in the search box in the bottom left corner), I assumed everything would work like normal - but I was mistaken. The javac command SEEMED to work. It flagged errors when I first tried to compile. After fixing the errors, I thought I had an error free compile. It was when I tried to run my "newly compiled" program that I noticed that my new changes were not showing! I discovered that - in spite of it seeming to compile - no new CLASS files were being created. (It was the old CLASS files that were being run.)
I was then advised to make sure I was the administrator. I did - with PowerShell and the Command Prompt - but it made no difference.
I tried reinstalling Java - but that made no difference either.
Here is what happens when I compile as administrator from the command prompt:
C:\Users\penny\Java>Set Path=C:\"Program Files"\Java\jdk1.8.0_131\bin
C:\Users\penny\Java>javac FoodCount.java
C:\Users\penny\Java>java FoodCount
Error: Could not find or load main class FoodCount
It compiles (or seems to), but no class file is created. And, yes, the javac.exe file IS in the bin file:
C:\Program Files\Java\jdk1.8.0_131\bin>dir javac.exe
Volume in drive C is Windows
Volume Serial Number is 7040-4F22
Directory of C:\Program Files\Java\jdk1.8.0_131\bin
07/12/2017 11:48 PM 15,904 javac.exe
1 File(s) 15,904 bytes
0 Dir(s) 838,527,008,768 bytes free
This is what happened when I used a suggestion from one of the comments for PowerShell. (This was before I reinstalled Java, so the version is a little different. After reinstalling, the result was exactly the same.)
PS C:\Users\penny\Java> $env:PATH += ';c:\Program
Files\Java\jdk1.8.0_102\bin'
PS C:\Users\penny\Java> javac FoodCount.java
javac : The term 'javac' 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
+ javac FoodCount.java
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (javac:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\penny\Java>
To sum up, I seem to be able to compile with javac.exe from the command prompt, but no new class file can be found in my directory afterwards. I cannot get PowerShell to recognize javac.exe at all.
Can anyone tell me what is going on? I think there must be something going on with the permissions, but being administrator doesn't seem to be enough.
I think Windows fixed this "bug" with another automatic update/patch!
When I tried compiling from the command prompt today, it worked! A class file was saved and I was able to run it. Yesterday, the compiler (javac.exe) seemed to work from the command prompt, but no new class file was created. I don't know why it would behave differently today - unless a Windows update fixed the problem.
I still can't get javac.exe to run in PowerShell, but I don't know what I am doing there.
If anyone else has this problem, my advice is PATIENCE. If you wait, it may go away!
I tried to install Posh-Git to use ssh with Powershell.
I followed the instructions here
The file here was not digitally signed so I changed the Execution Policy to install it. I then (for whatever reason) decided not to use this so I removed the file.
Now whenever I start Powershell I get an error similar to the below:
The term 'C:\xxx\xxx\profile.example.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.
How do I stop this error message from appearing each time I start Powershell?
You need to edit your PowerShell profile and remove that line from there. The easiest way would be (from within your PowerShell):
notepad $profile
or alternatively you can look up your profile with $profile and edit the file there.
Consult this for further reading: https://blogs.technet.microsoft.com/heyscriptingguy/2012/05/21/understanding-the-six-powershell-profiles/