> ## Content Index
> Fetch the complete content index at: https://alvis.org.cn/llms.txt
> Use this file to discover other available public pages before exploring further.

# 🥕 开机自动启动前端项目脚本
- URL: https://alvis.org.cn/de888a87/
- Published: 2024-10-25T09:32:58.000Z
- Updated: 2026-11-30T07:39:46.000Z
- Description: windows电脑开机自动启动前端项目的脚本
- Author: jie.chen
- Tags: 古法编程, 解决方案, 脚本, windows

> **前言 📝**  
>  
> 在公司中可能会有启动多个前端的需求，如果早晨到公司之后每个项目都运行一遍命令很麻烦。或者是周末的时候后端同事来加班，但是需要你的前端。你不能专门跑到公司给后端同事启动一个前端项目吧，这样太麻烦了。于是我就写了一个专属于windows上的脚本。

## 1\. 脚本

在widnwos系统中，`C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup`存放在该目录下面的`.bat`脚本会自动执行，但是`.ps1`脚本并不会自动执行。本片文章用`.ps1`脚本进行演示。

脚本(`start_chuming_plm6_project.ps1`)：

```bash
# 定义要搜索的根目录
$rootDir = "G:\xxxx"

# node版本
$nodeVersion = 14.20.0

# 获取所有以 "xxx" 开头的目录
$plm6Dirs = Get-ChildItem -Path $rootDir -Directory | Where-Object { $_.Name -like "xxx*" }

# 遍历每个目录
foreach ($dir in $plm6Dirs) {
    $projectDir = Join-Path $rootDir $dir.Name

    # 检查是否有 package.json 文件，确认是一个前端项目
    if (Test-Path (Join-Path $projectDir "package.json")) {
        Write-Host "start_project: $projectDir"
        
        # 使用 cmd /c 执行多个命令，并将窗口最小化
        Start-Process cmd.exe -ArgumentList "/c `"$env:ProgramFiles\nodejs\nvm\nvm.exe` use $nodeVersion && cd `"$projectDir`" && npm run dev" -WindowStyle Minimized
        
    } else {
        Write-Host "$projectDir not is project because package.json is not exist"
    }
}

```

![image.png](https://alvis.org.cn/content/images/2026/11/ce73bcfcdad69d9f59a44776.png)

## 2\. 设置开机自启

### 2.1 通过bat脚本去运行ps1脚本

1. 在 `C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup` 中创建一个新的 `.bat` 文件，比如 `RunMyScript.bat`。
2. 在 `.bat` 文件中添加以下内容：

```bash
powershell -ExecutionPolicy Bypass -File "C:\path\to\your\script.ps1"

```

其中，将 `C:\path\to\your\script.ps1` 替换为实际的 PowerShell 脚本路径。

1. 保存并关闭文件。这样，Windows 会在启动时自动运行 `.bat` 文件，从而间接启动你的 PowerShell 脚本。

> **注意：** `.bat`脚本只有在输入开机密码，登录之后才会开始运行。

### 2.2 使用任务计划程序来启动 PowerShell 脚本

1. 打开任务计划程序（可以在开始菜单中搜索 “Task Scheduler”）。
2. 点击 **创建任务**。

![image.png](https://alvis.org.cn/content/images/2026/11/f38503755e2d00d95e0ff989.png)

1. 在“常规”选项卡中，命名任务并选择 **使用最高权限运行**。

![image.png](https://alvis.org.cn/content/images/2026/11/0d972f3ec93f6b93cfb9d354.png)

1. 在“触发器”选项卡中，点击 **新建**，将触发器设置为 **在系统启动时**。

![image.png](https://alvis.org.cn/content/images/2026/11/50c531d5ae4337d2a6c7a002.png)

1. 在“操作”选项卡中，选择 **启动程序**，在“程序或脚本”中输入：

```bash
powershell

```

1. 在“添加参数”栏中，输入以下内容：

```bash
-ExecutionPolicy Bypass -File "C:\path\to\your\script.ps1"

```

将 `C:\path\to\your\script.ps1` 替换为实际脚本路径。

![image.png](https://alvis.org.cn/content/images/2026/11/19abdb7a3f75c68af8f48872.png)

1. 保存任务。这样，系统启动时（即登录前）脚本就会执行，不需要输入密码。