如何创建 PowerShell 配置文件
当 Windows PowerShell 启动时,将加载标准模块并将提示符的位置设置为 C:\Users\Username。如果您想加载其他模块、为命令创建别名或只是显示信息,那么创建 Powershell 配置文件是个好主意。
配置文件由 PS1 文件加载,该文件位于(创建后)用户的 Documents 文件夹中,并在 PowerShell 启动时加载。
检查先决条件
有必要验证执行策略是否允许运行 ps1 文件。执行策略应设置为 Remotesigned 或 Unrestricted(不推荐)。
Get-ExecutionPolicy如果策略配置为受限,则必须调整执行策略。
Set-ExecutionPolicy RemoteSigned为当前用户配置配置文件
现在可以使用 New-Item 创建该文件。
New-Item -ItemType File -Path $Profile -Force该文件存储在用户配置文件 C:\User\UserName\Documents\WindowsPowerShell 中。
为所有用户配置配置文件
如果您想为所有用户配置配置文件,请运行
New-Item -ItemType File -Path $PROFILE.AllUsersAllHosts -Force这将为所有用户和所有主机(PowerShell 和 Powershell ISE)配置配置文件。
创建后,使用 PowerShell ISE 将其打开。
ise $profile.AllUsersAllHosts编写脚本(当前用户)
在 Windows 资源管理器或 PS 中打开文件。对于编码,我推荐 PowerShell ISE。
ise $profile现在您可以自定义您的脚本。我准备了一个例子。复制代码并运行它。
Write-Host "Welcome to" (Invoke-Expression hostname) -ForegroundColor Green
Write-Host "You are logged in as" (Invoke-Expression whoami)
Write-Host "Today:" (Get-Date)
Set-Location c:\
New-Alias Time Get-Date -Force
Write-Host "PowerShell"($PSVersionTable.PSVersion.Major)"awaiting your commands."现在关闭 PowerShell 并再次打开它。
链接:https://technet.microsoft.com/de-at/library/ff461033.aspx
我的简历
最后,我想分享一下我的个人资料。我将位置设置为 C: 并缩小了窗口的大小。最后您可以看到 Start-Transcript。记录了所有的工作。
set-location c:\
cls
$Shell = $Host.UI.RawUI
$size = $Shell.WindowSize
$size.width=80
$size.height=34
$Shell.WindowSize = $size
$size = $Shell.BufferSize
$size.width=80
$size.height=3000
$Shell.BufferSize = $size
Start-Transcript有关 Start-Transcript 的更多信息,请参阅我的文章:PowerShell:使用 Start-Transcript 记录您的工作





