如何使用 python 打开 Powershell 到它的默认位置?

How to open Powershell with python to it's default location?

我正在尝试制作一个可以打开所有应用程序的数字助理。当我打开 PowerShell 时,目录设置为 python 脚本所在的位置。我已经准备好知道如何打开命令行到正确的文件夹我只需要 PowerShell。

我在用什么

我想做的是打开 PowerShell

C:\Users\MyUsername>

我试过的 我在 Powershell 中使用命令 'get-StartApps' 来获取所有应用程序 ID

这是我用来打开 PowerShell 的尝试

import os
os.system('start Powershell ')

os.startfile(r'C//Users//'+str(os.path.expandvars("%userprofile%"))+'//AppData//Roaming//Microsoft//Windows//Start Menu//Programs//Windows PowerShell//Windows PowerShell.lnk')

我不确定应用程序 ID 是否在所有计算机上都相同,所以我只举一个例子

import os
os.system('start explorer shell:appsfolder\{Id number}\WindowsPowerShell\v1.0\powershell.exe')

只是打开文件资源管理器,但需要打开其他应用程序。

我也希望它能在所有机器上运行,所以如果你知道如何请告诉我。

试试这个:

os.system('start /D "C:/Users" powershell')

启动命令的 /D 开关设置 powershell 打开的路径。

os.system('start /D'+ str(os.path.expandvars("%userprofile%"))+' powershell')