你可以这样运行它(但是这会显示一个窗口一段时间):
PowerShell.exe -WindowStyle hidden { your script.. }
或者您可以使用我创建的辅助文件来避免调用 PsRun.exe 窗口,该文件能够准确地实现这一点。您可以从 运行 PowerShell 中带有 WinForm GUI 的定时任务 下载源代码和可执行文件。我将其用于定时任务。
编辑:正如 Marco 所指出的那样,-WindowStyle
参数仅适用于版本 2 及以上。
-WindowStyle Hidden
参数就可以静默运行。 - adamcmd /c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Users\username\Desktop\test.ps1"
命令来避开窗口闪烁。 - Ste我发现,如果你去任务计划程序中的任务,并且该任务正在运行powershell.exe脚本,你可以点击“无论用户是否登录都运行”,这样任务运行时将不会显示powershell窗口。
缺点: 脚本无法与已登录用户的屏幕交互(例如显示通知或任何图形用户界面元素)。
前提条件: 用户必须具备"作为批处理作业登录"特权。
-WindowStyle Hidden
是很好的,但窗口仍然会闪烁。cmd /c start /min ""
调用窗口时会闪烁。cmd /c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Users\username\Desktop\test.ps1"
cmd /c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -Command ". 'C:\Users\username\Desktop\test me.ps1' -Arg1 'Hello' -Arg2 'World'"
Param
(
[Parameter(Mandatory = $true, HelpMessage = 'The 1st test string parameter.')]
[String]$Arg1,
[Parameter(Mandatory = $true, HelpMessage = 'The 2nd test string parameter.')]
[String]$Arg2
)
Write-Host $Arg1
Write-Host $Arg2
cmd /c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -Command ". 'C:\Users\username\Desktop\test me.ps1'; Get-Test -stringTest 'Hello World'"
function Get-Test() {
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true, HelpMessage = 'The test string.')]
[String]$stringTest
)
Write-Host $stringTest
return
}
%comspec%
作为程序/脚本调用,并将上述文件的调用代码作为参数。
注意:所有示例都适用于PS1文件路径中包含空格的情况。
ps1'; -Arg1 'Hello' -Arg2 ' World'"
。 - Peterstart-process PowerShell.exe -arg $pwd\foo.ps1 -WindowStyle Hidden
你也可以使用VBScript实现此操作:http://blog.sapien.com/index.php/2006/12/26/more-fun-with-scheduled-powershell/
计划隐藏的PowerShell任务(互联网档案馆)
更有趣的定时PowerShell任务(互联网档案馆)
(通过这个论坛帖子得知。)
这是一个一行代码:
mshta vbscript:Execute("CreateObject(""Wscript.Shell"").Run ""powershell -NoLogo -Command """"& 'C:\Example Path That Has Spaces\My Script.ps1'"""""", 0 : window.close")
虽然这可能会短暂地闪烁窗口,但这应该是一个罕见的情况。
$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
隐藏ps1文件,使其在任务计划程序和快捷方式中均不可见。
mshta vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -ExecutionPolicy Bypass & 'C:\PATH\NAME.ps1'"", 0:close")
我将此代码添加到我需要在后台运行的所有PowerShell脚本的开头。
# .Net methods for hiding/showing the console in the background
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
function Hide-Console
{
$consolePtr = [Console.Window]::GetConsoleWindow()
#0 hide
[Console.Window]::ShowWindow($consolePtr, 0)
}
Hide-Console
如果这个答案对你有帮助,请在此帖子中为"Bluecakes"的回答投票。
process.StartInfo = new ProcessStartInfo("powershell.exe",
String.Format(@" -NoProfile -ExecutionPolicy unrestricted -encodedCommand ""{0}""",encodedCommand))
{
WorkingDirectory = executablePath,
UseShellExecute = false,
CreateNoWindow = true
};
pythonw.exe
/javaw.exe
类型的解决方案)。 它正在GitHub上被(相当)积极地讨论,有几个关于在PowerShell或操作系统级别进行潜在解决方法/修复的建议。 因此,它可能会在“某一天”得到修补。 - desseim