【问题标题】:How-to add a line break in the terminal after running powershell code如何在运行 powershell 代码后在终端中添加换行符
【发布时间】:2019-01-09 20:11:57
【问题描述】:

有没有办法在已运行的完成脚本和 VSCode 中的下一个命令行提示符之间添加换行符。在 powershell 的 ISE 控制台中,它跳过了几行以使其更易于阅读。我浏览网页寻找答案,但我认为我的搜索措辞不正确。任何想法都会有所帮助。

【问题讨论】:

  • get-help about_prompts -Full

标签: powershell visual-studio-code


【解决方案1】:

如果您阅读了 Kory Gill 记录的 about_prompts 帮助文件(通过运行 get-help about_prompts -Full),那么这里有很多关于更改默认提示行为的示例。

运行(Get-Command Prompt).ScriptBlock 以获取默认布局。我的默认Prompt函数:

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

要在之前的代码输出后添加两个换行符,请运行:

Function Prompt {
"

PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
}

要使这些设置保持不变,您需要将修改后的函数添加到您的 PowerShell 配置文件中。 复制上面的代码(或您喜欢的代码)并将其添加到 C:\Users\User\Documents\WindowsPowershell\Profile.ps1 中的配置文件脚本中,如 about_Profiles 中所述。这将使所需的提示行为持续存在,并且还将应用于 vscode 中的嵌入式 PowerShell 终端。这些是about_Profiles 中记录的配置文件脚本位置:

Description                Path
-----------                ----
Current User, Current Host $Home\[My ]Documents\WindowsPowerShell\Profile.ps1
Current User, All Hosts    $Home\[My ]Documents\Profile.ps1
All Users, Current Host    $PsHome\Microsoft.PowerShell_profile.ps1
All Users, All Hosts       $PsHome\Profile.ps1

运行help about_Profiles -Full 以了解有关配置文件的更多信息。

【讨论】:

  • 感谢您向我解释。你知道我怎样才能在你提到的那个功能上添加另一个功能吗?也喜欢结合? function Prompt { Write-Host '[' -NoNewline Write-Host (Get-Date -UFormat '%m/$d/%y %T') -ForegroundColor Green -NoNewline Write-Host ']:' -NoNewline Write-Host (Get-Location | %{$_.path}) -NoNewline return "> " } set-location c:\ clear-host
  • 您可以在个人资料中添加任意数量的功能。不过我不知道你说的将两者结合是什么意思。您的意思是希望多个提示行为可用,并且您可以随意更改提示行为吗?
  • 好吧,我想在终端中有 "[01/10/19 15:04:27]:C:\>" 但也有你上面提到的换行符。在我的脑海中,我在想我必须将这两个功能结合起来才能实现这一点。还是vscode中不可能有的东西?
  • 请阅读帮助内容。你应该自己做一些研究,我和 Kory Gill 提到的帮助文件告诉你如何做这样的事情。有了这个,这是你需要的代码Function Prompt { $date = Get-Date -Format G "[$date]:$($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "; # .Link # https://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml }
猜你喜欢
  • 2015-01-19
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2022-12-07
  • 2021-10-02
  • 2020-09-08
  • 1970-01-01
相关资源
最近更新 更多