Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{command} is not recognized as an internal or external command, operable program or batch file. in PowerShell 7 #95

Open
JoanComasFdz opened this issue Aug 28, 2023 · 4 comments

Comments

@JoanComasFdz
Copy link

I do not seem to be able to run commands in PowerShell 7.3.6 in Windows Terminal 1.17.11461.0 on Windows 10 21H2 (19044.3208).

Example:

❯ ai get all image files

┌  AI Shell
│
◇  Your script:


Get-ChildItem -Path "C:\Path\to\folder" -Include *.jpg, *.png, *.gif -Recurse


•
│
◇  Explanation:


- Set the path to "C:\Path\to\folder"
- Include only files with extensions *.jpg, *.png, and *.gif
- Enable recursion to search for files in subfolders as well

•
│
◇  Run this script?
│  ✅ Yes
│
└  Running:
Get-ChildItem -Path "C:\Path\to\folder" -Include *.jpg, *.png, *.gif -Recurse



'Get-ChildItem' is not recognized as an internal or external command,
operable program or batch file.
@SaidTorres3
Copy link

Same problem

@jakenuts
Copy link

Short answer is to call this before ai-shell.

$env:SHELL="Powershell"

Long answer is:

AI-Shell uses execa to execute the generated scripts, and attempts to determine your shell by checking the process.env.SHELL environment variable. On Windows, that's undefined and so it passes "shell:true" which executes cmd.exe. (shell:false also fails so but I didn't dig further).

  • I'm still not satisfied with the short answer though as execa launches a new powershell process (loads your profile, modules, shows messages) which feels like overkill as you were just in a perfectly good Powershell process moments before you typed "ai". Still looking if there is a way to reuse your current shell process but getting sleepy.

@mefengl
Copy link

mefengl commented Nov 27, 2023

@jakenuts maybe run a PowerShell command to set the env value if it succeeds?

// by gpt4
const child_process = require('child_process');

function isRunningInPowershell() {
    try {
        // Execute a PowerShell-specific command
        child_process.execSync('Get-Process -Id $PID', { stdio: 'ignore' });
        return true;
    } catch (e) {
        return false;
    }
}

if (isRunningInPowershell()) {
    console.log("Running in PowerShell. Setting environment variable.");
    // Set the environment variable
    process.env.SHELL = "Powershell";
    console.log("Environment variable set:", process.env.SHELL);
    // Add any other PowerShell-specific operations here
} else {
    console.log("Not running in PowerShell, perform standard operations.");
    // Add standard operations here
}

@jakenuts
Copy link

jakenuts commented Nov 28, 2023

@mefengl Sure, that would likely work. Actually I just found another way suggested by someone on SO. Instead of trying to detect the shell inside the node project, create a ai.cmd and ai.ps1 file and when the user types "ai" each shell will call the one with it's default extension. Turns out, there already is an ai.cmd and ai.ps1 C:\Users{yourname}\AppData\Roaming\npm so you could probably just set the shell variable or pass an argument into the script from there.

image

I bet a clever PowerShell person (definitely not me) could even pass an argument into the ai-shell script to just return the script instead of using execa and (or copy it to the clipboard) then execute that output within the same PowerShell context you started in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants