powershell快捷键,使用PowerShell从快捷方式打开程序

本文探讨如何为PowerShell和CMD创建快捷方式,以在管理员模式下启动指定的服务器程序,使用相对路径并确保跨平台兼容。作者尝试了多种方法,包括使用代码编译执行,以实现最少的文件需求和适应性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a problem with creating a shortcut that opens a certain program in Powershell. It is part of a small game server. Therefore the server folder needs to be able to be copied, to create another server.

The program needs to be run in administrator mode. It needs to work for both cmd an powershell (I don't mind if they are 2 different shortcuts due to syntax). What I have so far:

CMD:

%SystemRoot%\System32\cmd.exe /c D: & cd "D:\Path\to\server\folder\" & Server.exe

PowerShell:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command "cd 'D:\Path\to\server\folder\';.\Server.exe"

The problem is that these paths aren't relative, so if I move the server folder, I would need to change the shortcut's target by replacing the path in the cd command.

It would be easier if I could use relative paths (the shortcut is located in the same folder as server.exe), but both the cmd as the PowerShell shortcut start in system32, because it is run in admin mode.

What can I do to make it work by creating at most 1 file for PS and 1 file for cmd (lnk, bat, ps1, I don't care)

EDIT: I also tried the following, but it didn't work:

C:\Windows\System32\cmd.exe /k cd & runas /user:\ server.exe

the problem is that first of all it asks me the password of the account (it is the same account), which is annoying. Secondly it opens the server in a separate window: one where I can't scroll or rightclick. And lastly, the folder it operates in is wrong, because it can not find files that are in the folder it should operate in.

Does anyone have a better idea?

解决方案

If you want the answer that isn't a workaround but uses c-code:

code for CMD:

#include

#include

int main()

{

system("C:\\Windows\\System32\\cmd.exe /k server.exe");

return 0;

}

code for PowerShell:

#include

#include

int main()

{

system("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -noexit -Command \"& '.\\server.exe'\"");

return 0;

}

compile and run in admin mode. Now there is weirdly enough no problem with the current folder.

It depends on your own preference if you want to have /c or /k (for cmd) and if you want -noexit (for PS)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值