After Microsoft fell in love with Linux , Power Shell , a component that was originally only available for Windows, was open sourced on August 18, 2016 and became a cross-platform software: https://linux.cn/article-7699-1.html , logged in Linux and macOS. |
PowerShell is an automation task and configuration management system developed by Microsoft. It is based on the .NET framework and consists of a command- line language interpreter (shell) and a scripting language.
PowerShell provides full access to COM (Component Object Model) and WMI (Windows Management Instrumentation), allowing system administrators to perform administrative tasks on local or remote Windows systems, as well as WS-Management and CIM ( Common Information Model) access to realize the management of remote Linux systems and network devices.
With this framework, administrative tasks are basically performed by .NET classes called cmdlets (pronounced command-lets). Just like Linux shell scripts , users can make scripts or executable files by writing a set of cmdlets into files according to certain rules . These scripts can be used as standalone command- line programs or tools.
Install PowerShell Core 6.0 on a Linux system
To install PowerShell Core 6.0 in Linux , we will use Microsoft repositories, which allow us to install through most popular Linux package manager tools like apt-get, yum, etc.
Install in Ubuntu 16.04
First, import the GPG key for this public repository, then register the Microsoft Ubuntu repository with APT 's sources to install PowerShell :
$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - $ curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list $ sudo apt-get update $ sudo apt-get install -y powershell
Install in Ubuntu 14.04
$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - $ curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list $ sudo apt-get update $ sudo apt-get install -y powershell
Install on CentOS 7
First, register the Microsoft RedHat repository to the YUM package manager repository list, then install PowerShell :
$ sudo curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/microsoft.repo $ sudo yum install -y powershell
How to use PowerShell Core 6.0 in Linux
In this section, we'll give a brief introduction to PowerShell ; we'll see how to start PowerShell, run some basic commands, and manipulate files, directories, and processes. Then learn how to list all available commands, display command help and aliases.
Enter the following command to start PowerShell:
$ powershell
Start PowerShell in Linux
You can check the PowerShell version with the following command:
$PSVersionTable
Check PowerShell version
Run basic PowerShell commands in Linux.
get-date [# show current date] get-uptime [# show uptime] get-location [# show current working directory]
Manipulate files and directories in PowerShell
1. There are two ways to create an empty file:
new-item tecmint.tex or "">tecmint.tex
Then add content to it and view the file content.
set-content tecmint.tex -value "TecMint Linux How Tos Guides" get-content tecmint.tex
Create a new file in PowerShell
2. Delete a file in PowerShell
remove-item tecmint.tex get-content tecmint.tex
Delete a file in PowerShell
3. Create a directory
mkdir tecmint-files cd tecmint-files “”>domains.list ls
Create a directory in PowerShell
4. Execute long-format list operation, list file/directory details, including mode (file type), last modification time, etc., use the following command:
dir
List long list of directories in Powershell
5. Display all processes in the system:
get-process
Show running processes in PowerShell
6. View the details of the running process/process group through the given name, and pass the process name as a parameter to the above command, as follows:
get-process apache2
View the specified process in PowerShell
The meaning of each part in the output:
- NPM(K) – non-paged memory used by the process, unit: Kb.
- PM(K) – Pageable memory used by the process, unit: Kb.
- WS(K) – The size of the working set of the process, unit: Kb, the working set consists of the memory pages referenced by the process.
- CPU(s) – Processor time, in seconds, used by the process on all processors.
- ID - Process ID (PID).
- ProcessName – Process name.
7. To learn more, get a list of PowerShell commands:
get-command
List PowerShell commands
8. If you want to know how to use a command, check its help (similar to man in Unix/Linux); for example, you can get the help of the command Describe like this :
get-help Describe
PowerShell Help Manual
9. To display the aliases of all commands, enter:
get-alias
List PowerShell command aliases
10. Finally, but not least, display the command history (list of previously run commands):
history
Show PowerShell command history
That's all! In this article, we showed how to install Microsoft PowerShell Core 6.0 in Linux . In my opinion, PowerShell has come a long way compared to traditional Unix/Linux shells. At present, it seems that PowerShell also needs to provide better, more exciting and productive features in command line operation machine, and more importantly, programming (writing scripts).
Check out the GitHub repository for PowerShell: https://github.com/PowerShell/PowerShell.
About the Author:
Aaron Kili is a Linux and FOSS fanatic, future Linux system administrator, web developer, and currently a content editor at TecMint, a person who loves computing and is committed to sharing knowledge.