Skip to content

Windows

进入当前C盘用户目录

bash
%USERPROFILE%\AppData\Roaming\preprocessing

文件和目录操作

bash
Get-ChildItem            # 列出当前目录内容(类似 dir/ls)
Set-Location <>      # 更改目录(类似 cd)
New-Item <文件> -ItemType File  # 创建新文件
New-Item <目录> -ItemType Directory # 创建新目录
Remove-Item <文件/目>  # 删除项目
Copy-Item <> <>    # 复制文件/目录
Move-Item <> <>    # 移动文件/目录

创建profile

bash
New-Item -Path $PROFILE -Type File -Force

1. 命令分解

参数作用示例值
New-Item创建新项目(文件、目录等)-
-Path $PROFILE指定文件路径($PROFILE 是 PowerShell 配置文件路径)C:\Users\用户名\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
-Type File指定创建的类型是 文件(不是目录)FileDirectory
-Force强制创建:如果文件已存在则覆盖,如果目录不存在则自动创建父目录-

2. 命令的作用

  • 如果 $PROFILE 文件不存在: 创建一个空的 PowerShell 配置文件(如 Microsoft.PowerShell_profile.ps1)。
  • 如果 $PROFILE 文件已存在: 强制覆盖该文件(原内容会被清空,慎用!)。

查看文件内容命令Get-Content(gc)

方法 1:直接打印内容(适用于短文件)

bash
Get-Content $PROFILE
  • 作用:显示 $PROFILE 文件的所有内容。

  • 示例输出

    bash
    # 这是我的 PowerShell Profile
    Set-Alias ll Get-ChildItem
    function Greet { Write-Host "Hello!" }

方法 2:分页查看(适用于长文件)

bash
Get-Content $PROFILE | more
  • 作用:逐页显示内容,按 空格键 翻页,按 Q 退出。

方法 3:带行号查看

bash
Get-Content $PROFILE -ReadCount 0 | ForEach-Object { "[{0}] {1}" -f ($i++), $_ }
  • 作用:显示每行的行号,方便定位。

  • 示例输出

    bash
    [0] # 这是我的 PowerShell Profile
    [1] Set-Alias ll Get-ChildItem
    [2] function Greet { Write-Host "Hello!" }

方法 4:用记事本打开

bash
notepad $PROFILE
  • 作用:用 Windows 记事本打开 $PROFILE 文件,方便编辑。

设置变量

常用目录

bash
cd $env:USERPROFILE    # 用户目录
cd $env:APPDATA        # AppData\Roaming
cd $env:LOCALAPPDATA   # AppData\Local
cd $env:TEMP           # 临时文件夹

临时设置变量

bash
$docs = "C:\Users\$env:USERNAME\Documents"
cd $docs

设置当前路径的变量

bash
# 将当前路径保存到变量 $myPath
$myPath = $pwd
# 或
$myPath = Get-Location

# 查看变量内容
$myPath

# 稍后切换回该路径
Set-Location $myPath
# 或简写
cd $myPath

如何删除临时变量?

如果临时变量不再需要,可以手动清除:

bash
Remove-Variable -Name myPath  # 彻底删除变量
# 或
$myPath = $null              # 清空变量值

永久变量(跨会话有效)

若希望变量在 所有新开的 PowerShell 窗口 中都能使用,需将其添加到 PowerShell 配置文件$PROFILE):

bash
# 打开配置文件
notepad $PROFILE

# 在文件中添加以下内容并保存
$myPath = "C:\Your\Default\Path"  # 替换为你的常用路径
  • 生效条件:重启 PowerShell 后,$myPath 会自动加载。
  • 验证方法:新开 PowerShell 窗口,输入 $myPath 应能正常显示路径。

添加环境变量

bash
$env:Path += ";C:\Users\user\AppData\Local\Programs\oh-my-posh\bin"
  • $env:Path 这是 PowerShell 中访问 PATH 环境变量的方式,它存储了一系列目录路径,系统会在这些路径中查找可执行文件。
  • += 表示 追加 内容到现有变量,而不是覆盖它。
  • ; 在 Windows 中,PATH 环境变量的不同路径之间用分号 ; 分隔。
  • C:\Users\user\AppData\Local\Programs\oh-my-posh\bin 这是 Oh My Posh 的安装路径(bin 目录通常存放可执行文件)。

查看系统端口占用

plaintext
   netstat -ano | findstr [端口号]

shell
PS C:\Users\lw> netstat -ano | findstr "3000"
  TCP    127.0.0.1:3000         127.0.0.1:7890         ESTABLISHED     33780
  TCP    127.0.0.1:7890         127.0.0.1:3000         ESTABLISHED     30108
  TCP    192.168.5.228:13973    103.212.12.43:3000     ESTABLISHED     8668

ESTABLISHED就是进程的pid

杀掉进程

plaintext
taskkill /pid [进程 ID] /f

shell
PS C:\Users\lw> taskkill /pid 33780 /f
SUCCESS: The process with PID 33780 has been terminated.

oh-my-posh

bash
 notepad $PROFILE

info 信息

When the above command gives an error, make sure to create the profile first. 当上述命令出现错误时,请确保先创建配置文件。

powershell
New-Item -Path $PROFILE -Type File -Force

In this scenario, it can also be that PowerShell blocks running local scripts. To solve that, set PowerShell to only require remote scripts to be signed using Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine, or sign the profile. 在这种情况下,也可能是 PowerShell 阻止运行本地脚本。要解决此问题,请将 PowerShell 设置为仅要求使用 对远程脚本进行 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine 签名,或对配置文件进行签名 。Add the following snippet as the last line to your PowerShell profile script: 将以下代码片段作为 PowerShell 配置文件脚本的最后一行添加:

powershell
oh-my-posh init pwsh | Invoke-Expression

✅ 解决方案:将初始化命令加入 PowerShell 配置文件

我们只需要把那行命令加到 PowerShell 的配置文件中(也就是 $PROFILE 文件):

1️⃣ 打开 PowerShell,执行以下命令:

powershell


复制编辑
notepad $PROFILE

如果提示找不到文件,可以先创建:

powershell复制编辑New-Item -Path $PROFILE -ItemType File -Force
notepad $PROFILE

2️⃣ 在打开的文件中添加你这行配置命令:

powershell


复制编辑
oh-my-posh init pwsh --config "$HOME/custom.omp.json" | Invoke-Expression

⚠️ 注意:

  • ~ 在 PowerShell 中并不会自动解析为 $HOME,所以建议改成 $HOME 或完整路径,例如:

    powershell
    
    
    复制编辑
    oh-my-posh init pwsh --config "C:\Users\你的用户名\custom.omp.json" | Invoke-Expression

3️⃣ 保存并关闭 notepad,然后重新打开一个 PowerShell 终端窗口

这时应该自动加载你的主题,不需要手动运行了。

安装wls

1.** 检查是否启用了 WSL 和虚拟机平台功能**

bash
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

然后重启电脑。

2. 更新 WSL 到最新版本

确保你的 WSL 内核是最新的:

bash
wsl --update

如果你无法更新,可以手动下载并安装最新内核包:

安装ubuntu系统

bash
# 查看可用的分发版本
wsl --list --online
# 安装
wsl --install -d Ubuntu-24.04

更改ubuntu位置

1. 关闭Ubuntu系统

运行PowerShell,输入wsl --shutdown。再输入wsl -l -v查看Ubuntu-22.04的运行状态,STATEStopped即说明已关闭。

powershell
wsl --shutdown
wsl -l -v

2. 创建目录。

在合适的磁盘上创建一个目录,例如E盘的E:\Ubuntu2204

3. 导出镜像。

在PowerShell中输入下列命令,将Ubuntu导出到指定目录,并等待操作完成。

powershell
wsl --export Ubuntu-24.04 E:\Ubuntu2404.tar

4. 注销原系统。

在wsl中注销原有的Ubuntu系统,powershell中输入wsl --unregister Ubuntu-22.04。完成后输入wsl -l -v查看,提示适用于 Linux 的 Windows 子系统没有已安装的分发。即为卸载成功。

powershell
wsl --unregister Ubuntu-24.04
wsl -l -v

5. 导入镜像。

在powershell中输入wsl --import Ubuntu-22.04 E:\Ubuntu2204 E:\Ubuntu2204.tar,将之前导出的镜像导入到新的Ubuntu系统。在powershell中输入wsl -l -v查看,出现Ubuntu-22.04的NAME即为导入成功。

powershell
wsl --import Ubuntu-24.04 E:\Ubuntu2404 E:\Ubuntu2404.tar
wsl -l -v

6. 更改默认用户。

在powershell中输入ubuntu2204.exe config --default-user [YourUesrName],将新的Ubuntu系统默认用户设置为[YourUesrName]

如更改默认用户为tdc,则输入下列命令:

powershell
ubuntu2204.exe config --default-user tdc

7. 启动Ubuntu子系统。

在开始菜单中搜索Ubuntu并运行,出现终端界面即为成功。

8. 删除文件。

删除导出的镜像文件E:\Ubuntu2204.tar

Tips: 可以通过上述方法备份和恢复Ubuntu系统

Tips: 在Windows的资源管理器中,按住Shift键,右键空白处或者文件夹,即可选择在此处打开 Linux shell,从而实现快速启动Ubuntu子系统并切换终端目录。

Tips: 在Windows的资源管理器中,输入 \\wsl$ 即可访问Ubuntu的文件系统,该系统是通过网络接口访问的,可右键该文件夹并选择 映射网络驱动器 将其映射为带有盘符的目录,从而实现快速访问Ubuntu文件