More

    How to use PowerShell on Linux

    You can use PowerShell on Linux the same way you can use any other shell such as Bash or Zsh. This is important if you are used to Windows PowerShell and moving over to the open-source world with Linux as your operating system of choice.

    In most cases, several PowerShell commands can’t work on Linux by default, but with Microsoft’s recent embracement of the open-source world and a key acquisition of Github, there is a collection of commands you can still run in the default shell.

    Well, to fully use PowerShell on Linux, all you need is to have the latest package installed, then follow the prompts below.

    Prerequisites

    Before you begin, please make sure you:

    To use PowerShell on Linux, below are some key commands you can run to verify that the Shell is running well without error.

    • Get-ChildItem: Lists the contents of a directory. By default, it shows the contents of your current directory, if you pass a directory path, then, it will display its contents.
    • Get-Process: Lists processes that are running on the system. It provides information such as the process name, process ID, and CPU usage.
    • New-Item: Creates a new file or directory. It takes the path and the name of the new item as parameters. If used with no arguments, the command defaults to creating a file.
    • Set-Location: Changes the current working directory. It takes the directory as a parameter.
    • Get-Help: Displays help information for a given command. You can use it to learn about the syntax, parameters, and examples for any Powershell command.
    • Get-date: Displays the current system date.

    Unlike Linux, PowerShell commands are not case sensitive, whether you use lowercase or uppercase, the command means the same, and below is how you can use PowerShell on Linux by executing available commands.

    Use PowerShell on Linux

    1. Start PowerShell on Linux.
        $ pwsh         
    1. Check the installed PowerShell version on your Linux system.
        > $PSVersionTable

    Output:

    Name                           Value
    ----                           -----
    PSVersion                      7.3.3
    PSEdition                      Core
    GitCommitId                    7.3.3
    OS                             Linux 4.15.0-1113....
    Platform                       Unix
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    1. Check how long your Linux system has been running.
    > get-uptime
    1. Check the current Linux system time.
    > get-date
    1. Check your current directory path.
    > get-location 
    1. To create a new directory, run the following command.
    > New-Item -ItemType Directory -Name "myfiles"
    1. Switch to the directory.
    > Set-Location myfiles
    1. Create a file in the directory.
    > New-Item example.txt

    Output:

     Directory: /home/example/myfiles
    
    UnixMode   User             Group                 LastWriteTime           Size Name
    --------   ----             -----                 -------------           ---- ----
    -rw-rw-r-- example             ex                  3/8/2023 20:36              0 example.txt
    
    1. Add some basic content to the file.
    > Add-Content -Path "example.txt" -Value "Hello World! This is my first text file."
    1. View the file contents.
    > get-content example.txt
    1. Create another file.
    > New-Item file2.txt
    1. Add some text to the file.
    > Add-Content -Path "file2.txt" -Value "Hello World! This is my second text file."
    1. View the file contents.
    > get-content file2.txt
    1. View all directory contents in a long listing.
    > dir

    Output:

    Directory: /home/example/myfiles
    
    UnixMode   User             Group                 LastWriteTime           Size Name
    --------   ----             -----                 -------------           ---- ----
    -rw-rw-r-- example              ex                  3/8/2023 20:36              0 example.txt
    -rw-rw-r-- example             ex                  3/8/2023 20:44              0 file2.txt
    1. Delete the last file you created.
    > Remove-Item "file2.txt"
    1. Show a list of more commands you can run in the Shell.
    > Get-command

    Output:

    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Function        cd..
    Function        cd\
    Function        Clear-Host
    Function        Compress-Archive                                   1.2.5      Microsoft.Powe…
    Function        exec
    Function        Expand-Archive                                     1.2.5      Microsoft.Powe…
    Function        Find-Command                                       2.2.5      PowerShellGet
    Function        Find-DSCResource                                   2.2.5      PowerShellGet
    Function        Find-Module                                        2.2.5      PowerShellGet
    Function        Find-RoleCapability                                2.2.5      PowerShellGet
    Function        Find-Script                                        2.2.5      PowerShellGet
    Function        Get-CredsFromCredentialProvider                    2.2.5      PowerShellGet
    1. Now, view a list of all command aliases, to enter minimal text for your commands.
    Get-alias

    Output:

    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Alias           ? -> Where-Object
    Alias           % -> ForEach-Object
    Alias           cd -> Set-Location
    Alias           chdir -> Set-Location
    Alias           clc -> Clear-Content
    Alias           clhy -> Clear-History
    Alias           cli -> Clear-Item
    Alias           clp -> Clear-ItemProperty
    Alias           cls -> Clear-Host
    Alias           clv -> Clear-Variable
    Alias           copy -> Copy-Item
    1. Use the aliased cd command to move to your home directory.
    > cd
    1. Use the aliased ls command to list items in your directory.
    > ls
    1. Exit the PowerShell.
    > exit

    Conclusion

    You have successfully used PowerShell on Linux by running a few basic commands on your Linux system. As listed above, you can create files, delete files, make directories, and also run short commands as listed by their aliases.

    If you’d like to change your default Linux shell to PowerShell, then, change your user account shell entry in the .htpasswd file. For more information on using PowerShell on Linux, visit the official project GitHub repository.

    JUST IN

    Subscribe
    Notify of
    guest

    0 Comments
    Inline Feedbacks
    View all comments

    Read Also....