# An Experience with Powershell Configuration

> This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to [original Chinese version](<https://blog.wendain.ing/posts/a-powershell-configuration-problem/>). I am continuously working to improve the translation.

## Preface

As I mentioned before, my blog deployment is done through a git workflow:

- The `Blog-Source` branch stores the source files
- The `main` branch stores the generated static pages
- CI/CD listens for pushes to `Blog-Source`, automatically runs `hexo g`, and then deploys to `main`

For convenience, I added a PowerShell function as an alias for `git add && git commit && git push`, named `blogdeploy`.

But this is actually the new version. Before I had this workflow, my `blogdeploy` was just `hexo g` and the like.

Then, considering my actual needs, I wanted to add a `git pull` at the very beginning, but I was too lazy to look into it myself (and honestly, I don't really understand PowerShell either), so I handed the task over to an Agent.

And then the following dramatically hilarious thing happened:

## 1. Looking at the Wrong File

The AI searched for `$PROFILE` for me and found a `Microsoft.PowerShell_profile.ps1` under `~\Documents\WindowsPowerShell\`, which did indeed contain `blogdeploy`, with the content:

```powershell
function blogdeploy {
    cd "C:\Users\endle\hexo-blog"
    hexo clean
    hexo g
    hexo d
}
```

There were no git operations at all. And this completely contradicted my memory, because I remembered seeing git commit / push output when running `blogdeploy`.

But the AI still added `git pull` for me.

It was only when I ran it again that I noticed the output looked roughly like this:

```powershell
🚀 提交并推送...
[Blog-Source 863d366] Site updated: 2026-06-21 00:27:14
...
To https://github.com/wendaining/wendaining.github.io.git
   Blog-Source -> Blog-Source
✅ 推送完成！CI 将自动编译部署。
```

No different from what I remembered, but `git pull` was never actually added.

## 2. Finding the Problem

Only after re-investigating did I find the root cause: I had previously set up Windows to **redirect the Documents folder to the D drive**: `~\Documents  →  D:\UserFolders\Documents`.

Meanwhile, PowerShell actually has two completely independent profile systems:

|              | Windows PowerShell 5.1                        | PowerShell 7 (`pwsh`)                  |
| ------------ | --------------------------------------------- | -------------------------------------- |
| Profile directory | `Documents\WindowsPowerShell\`                | `Documents\PowerShell\`                |
| Actual path     | `D:\UserFolders\Documents\WindowsPowerShell\` | `D:\UserFolders\Documents\PowerShell\` |

I'm using PowerShell 7, so the real configuration is in the `PowerShell\` directory. The one the AI found under `WindowsPowerShell\` was an **old version left over from when I used Windows PowerShell 5.1 in the early days**, long since abandoned.

The real `blogdeploy` has actually always looked like this:

```powershell
function blogdeploy {
    cd "C:\Users\endle\hexo-blog"
    # git add → commit → push → CI 自动部署
    git add .
    git commit -m "Site updated: $currentTime"
    git push origin Blog-Source
}
```

There was never any `hexo clean/g/d` — those all run in CI/CD.

## 3. Summary

This was caused by the combination of three factors:

1. **Documents redirection** — `C:\Users\{Username}\Documents` is mapped to `D:\UserFolders\Documents`. The AI's initial search didn't cross-validate, didn't use the `$PROFILE` variable, and instead guessed the path.
2. **Dual PowerShell versions** — PowerShell 5.1 and PowerShell 7 each have their own independent profile paths, with no overlap between them.
3. **Old configuration never cleaned up** — The hexo version of `blogdeploy` written back when I used Windows PowerShell 5.1 kept lingering in the old path. After switching to PW7, I rewrote the git version, but never deleted the old one.

Adding it to the correct file (`D:\UserFolders\Documents\PowerShell\Microsoft.PowerShell_profile.ps1`) solved the problem:

```powershell
function blogdeploy {
    cd "C:\Users\endle\hexo-blog"

    Write-Host "🔄 拉取最新代码..." -ForegroundColor Cyan
    git pull

    if ($LASTEXITCODE -ne 0) {
        Write-Host "❌ git pull 失败，请检查冲突或网络。" -ForegroundColor Red
        return
    }

    # ... 后续 git add / commit / push
}
```

At the same time, I deleted that long-abandoned hexo version of `blogdeploy` under WindowsPowerShell.

## 4. But It's Not Over Yet

I was actually quite curious: I clearly migrated Documents to the D drive via "right-click → Properties → Location", so why is there still a `Documents` directory on the C drive, with files inside it no less?

What I discovered next is Windows' dramatically hilarious legacy mess.

Windows' folder redirection mechanism is extremely simple and crude:

```TEXT
Before migration: ~\Documents  ← actual storage
After migration:  D:\UserFolders\Documents       ← actual storage (registry points here)
                   ~\Documents  ← empty shell, physical directory left behind
```

The key detail: **Windows does not create a symbolic link at the old path**. The old `C:\Users\<用户名>\Documents` directory just stays there, degenerating into an ordinary folder.

The proper approach would actually be to go through the Shell API and query the registry to get the current real Documents directory.

**But** even many of Microsoft's own products (such as VS) simply hardcode the path `$env:USERPROFILE + "\Documents"` instead of querying the API, which causes these programs to keep writing files to that completely wrong path.
