Niklas Blog

Exploring Device Management.

PowerShell Profile: customize your terminal and add functions

31 December 2023

Personalizing your PowerShell experience

If you’re a Windows system administrator, you’re probably familiar with PowerShell. PowerShell is a powerful command-line tool that lets you automate tasks and manage Windows systems. However, did you know that you can customize PowerShell to make it even more powerful? One way to do this is by using a PowerShell profile which I will show you in this post.

What is a PowerShell Profile?

A PowerShell profile is a script that runs every time you start a PowerShell session. It allows you to customize your PowerShell environment, including the prompt, aliases, and functions. When you create a PowerShell profile, you can save time by automating tasks you frequently perform or creating shortcuts for common commands.

There are two types of PowerShell profiles:

The user profile applies only to the current user, while the system profile applies to all users on the system. By default, PowerShell looks for user profiles in the $Home\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 directory and system profiles in the $PsHome\Microsoft.PowerShell_profile.ps1 file.

Create your PowerShell Profile

Follow this quick guide to create your own PowerShell profile.

  • Open PowerShell: Open PowerShell by typing PowerShell in the Windows search bar and clicking on Windows PowerShell from the results.
  • Check if a profile already exists: Before creating a new profile, check if one already exists. To do this, run the command Test-Path $PROFILE. This command will return True if a profile already exists and False if it doesn’t.
  • Create a new profile: If a profile doesn’t exist, create a new one by running the command New-Item -ItemType File -Force $PROFILE. This command will create a new file called Microsoft.PowerShell_profile.ps1 in your user profile directory.
  • Edit the profile: To edit the profile, run the command notepad $PROFILE (or open the file in your prefered Editor like for example VS Code). This will open the profile file in the Editor.
  • Add commands to the profile: In the profile file, you can add any PowerShell commands that you want to run every time you open PowerShell. For example, you might want to add an alias for a frequently used command or load a module that you use regularly.
  • Save the profile: Once you have added your commands to the profile file, save it and close the text editor.
  • Test the profile: To test that the profile is working correctly, close and reopen PowerShell. Your commands should be executed automatically when PowerShell opens.

That’s it! You now have a PowerShell profile that will run your custom commands every time you open PowerShell.

Use Cases

Within your PowerShell profile you can customize or add many things. Here are some examples:

  • Add custom functions: You can define custom PowerShell functions in your profile and use them in your PowerShell sessions. For example, you could define a function to connect to a remote server, or a function to automate a common task.
  • Customize your console: You can customize your PowerShell console by changing the font, colors, and other settings. You can also add custom prompts or modify the default prompt to display more information.
  • Set default preferences: You can set default PowerShell preferences in your profile, such as the default output format or the default execution policy.
  • Load modules automatically: You can load PowerShell modules automatically in your profile, so you don’t have to remember to import them each time you start PowerShell.

Conclusion

So, in conclusion an IT admin can benefit from using a PowerShell profile in several ways:

  • Personalization: A PowerShell profile is a script that runs every time PowerShell starts. It allows the IT admin to customize their PowerShell environment with their preferred settings, aliases, and functions. This can improve productivity and efficiency by reducing the need for repetitive tasks.
    • Consistency: A PowerShell profile ensures that the same settings and commands are available every time PowerShell is launched, regardless of the machine or user account. This can help maintain consistency across different machines and make it easier to troubleshoot issues. Automation: PowerShell profiles can be used to automate repetitive tasks or to configure the PowerShell environment to interact with other applications and systems. This can help save time and reduce errors.
  • Security: A PowerShell profile can be used to set security policies, such as disabling certain commands or restricting access to certain resources. This can help protect the system and prevent unauthorized access.

Overall, using a PowerShell profile can help IT admins personalize their PowerShell environment, maintain consistency across machines, automate tasks, and improve security. As it can be extended by using PowerShell modules to interact with more systems it simplifies everyday work.