When Windows PowerShell starts, standard modules are loaded and the location of the prompt is set to C:\Users\Username. If you want to load additional modules, create an alias for a command, or simply display information, then it´s a good idea to create a Powershell Profile.
The profile is loaded by a PS1 file, which is located (once it is created) in the users Documents folder and is loaded at startup of PowerShell.
Checking Prerequisites
It is necessary to verify that the Execution Policy allows to run ps1 files. The Execution Policy should be set to either Remotesigned or Unrestricted (not recommended).
Get-ExecutionPolicy
If the policy is configured to Restricted, then the Execution Policy must be adjusted.
Set-ExecutionPolicy RemoteSigned
Configuring a Profile for the current user
Now the file can be created with New-Item.
New-Item -ItemType File -Path $Profile -Force
The file is stored in the user profile C:\User\UserName\Documents\WindowsPowerShell.
Configure a Profile for all users
If you want to configure a profile for all users run
New-Item -ItemType File -Path $PROFILE.AllUsersAllHosts -Force
This will configure a profile file for all users and all hosts (PowerShell and Powershell ISE).
Once created open it with PowerShell ISE.
ise $profile.AllUsersAllHosts
Writing the Script (Current User)
Open the file in Windows Explorer or in PS. For coding, I recommend PowerShell ISE.
ise $profile
Now you can customize your script. I have prepared an example. Copy the code and run it.
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."
Now close PowerShell and open it again.
Link: https://technet.microsoft.com/de-at/library/ff461033.aspx
My Profile
Finally, I want to share my profile. I set the location to C: and I reduced the size of the window. At the end you can see Start-Transcript. That records all the work on.
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
More about Start-Transcript in my article: PowerShell: Documenting your work with Start-Transcript
Categories: PowerShell
If you are running PowerShell 5 it might be beneficial to enable systemwide transcription instead of manual transcription using Start-Transcript. You can enable this by setting the following registry keys:
Set-ItemProperty ‘HKLM:\Software\Policies\Microsoft\Windows\PowerShell\Transcription’ -Name EnableTranscripting -Value 1
Set-ItemProperty ‘HKLM:\Software\Policies\Microsoft\Windows\PowerShell\Transcription’ -Name OutputDirectory -Value c:\yourfolder
Then you won’t require the Start-Transcript in your profiel anymore, for more information about PowerShell logging have a look over here:
https://blogs.msdn.microsoft.com/powershell/2015/06/09/powershell-the-blue-team/
LikeLike
Hi Jaap!
Thank you for your comment and useful advice. To enable systemwide transcripting you can also use Group Policy on the localhost or in Active Directory. It´s the same as editing the registry.
Regards,
P
LikeLike