You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改VSCode运行按钮的执行行为?运行PS1/CMD脚本

VSCode运行按钮自定义配置与脚本执行指南

一、编辑VSCode运行按钮的执行操作

VSCode的运行按钮行为由launch.json配置文件控制,操作步骤如下:

  1. 打开「运行与调试」面板(快捷键Ctrl+Shift+D)
  2. 点击面板顶部的「创建launch.json文件」,根据需求选择对应环境(比如选「PowerShell」或「Others」生成基础模板)
  3. 在生成的launch.json中,修改configurations数组内的配置项:
    • name:自定义运行按钮显示的名称
    • type:指定运行环境(如PowerShell、cppvsdbg对应CMD环境)
    • request:固定设为launch表示启动新进程
    • script/program:指定要执行的脚本或程序路径
    • args:传递给脚本/程序的参数
    • cwd:设置执行时的工作目录

二、点击运行按钮执行.PS1或.CMD脚本

1. 执行PowerShell(.PS1)脚本

在launch.json中添加以下配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "运行PowerShell脚本",
            "type": "PowerShell",
            "request": "launch",
            "script": "${file}", // 运行当前打开的PS1文件,可替换为绝对路径如"G:\\Devving\\Main\\Shell\\StartMain.ps1"
            "args": [],
            "cwd": "${fileDirname}"
        }
    ]
}

将目标代码保存为.ps1文件,打开该文件后点击运行按钮即可执行。

2. 执行CMD(.CMD)脚本

在launch.json中添加以下配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "运行CMD脚本",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd.exe",
            "args": ["/c", "${file}"], // /c参数表示执行脚本后自动关闭CMD窗口
            "cwd": "${fileDirname}"
        }
    ]
}

针对你提供的PowerShell代码的配置示例

将代码保存为StartMain.ps1文件:

$path = 'G:\Devving\Main\Shell'
$file = 'Main.py'

$cmd = $path+"\\"+$file
Start-Process $cmd

然后在launch.json的「运行PowerShell脚本」配置中,将script路径指向该文件,点击运行按钮即可启动指定路径下的Main.py。

内容的提问来源于stack exchange,提问作者Dragon75240

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 19:15:44