2023-12-23

Windows PowerShell Configuration

Windows PowerShell is a command-line shell and scripting language designed for system administration tasks. It is built on the .NET Framework and is included in Windows operating systems. PowerShell provides a powerful and flexible command-line interface that allows you to automate tasks and manage your system more efficiently.

A PowerShell profile is a script that runs when PowerShell starts, and it can be used to customize your environment. The path to the user's profile script is stored in the $PROFILE variable, as shown in the video below.

To create a profile, if doesn't exist, you can use the following command:

if (!(Test-Path -Path $PROFILE)) {
  New-Item -ItemType File -Path $PROFILE -Force
}

Inside the profile's script it is possible to create a set of aliases for executables that are not in the user's path. If you have Vim installed in a non-standard location, you can create an alias for it using the following command:

   Set-Alias vim "C:\Users\Seve\Vim\vim90\vim.exe"

This will allow you to open the text editor automatically by simply typing Vim in PowerShell.

By adding the code below to the user's profile, you can run the command libreoffice to open Office and LibreOffice files from the PowerShell without LibreOffice's splash screen at the start of the program.

    Set-Alias libreoffice lo

    function lo() {
        param(
            [string]$file
            )

        # Get the current working folder
            $folder = Get-Location

        # Combine the folder and the file name into a full path
            $path = Join-Path $folder $file

        # Open the file with LibreOffice
            Start-Process -FilePath "C:\Program Files\LibreOffice\program\soffice.exe" -ArgumentList "--nologo", $path
        
    }

For example, if you have a file called example.docx and you want to open it with LibreOffice, you can use the following command:

   PS C:\Users\Seve> libreoffice example.docx