PowerShell for Linux : Install2026/01/07 |
|
Install Microsoft PowerShell for Linux. |
|
| [1] | |
| [2] | Install Microsoft PowerShell. |
|
dlp:~ # snap install powershell --classic powershell 7.5.4 from Canonical** installed |
| [3] | This is the Bbsic usage of PowerShell. |
|
# run PowerShell dlp:~ # pwsh PowerShell 7.5.4 PS /root> # display Cmdlet list (only display 10 lines from the head) PS /root> (Get-Command)[0..9] | Format-Table -AutoSize CommandType Name Version Source ----------- ---- ------- ------ Alias Get-PSResource 1.1.1 Microsoft.PowerShell.PSResourceGet Function cd.. Function cd\ Function cd~ Function Clear-Host Function Compress-Archive 1.2.5 Microsoft.PowerShell.Archive Function exec Function Expand-Archive 1.2.5 Microsoft.PowerShell.Archive Function Find-Command 2.2.5 PowerShellGet Function Find-DSCResource 2.2.5 PowerShellGet # display the current PATH PS /root> pwd Path ---- /root # change directory to /home PS /root> cd /home # back to home PS /home> cd # display files under the current direcroty (dir equals Get-ChildItem) PS /root> dir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/28/2025 18:26 0 bin drwx------ root root 01/07/2026 08:59 20 snap # display files under / PS /root> Get-ChildItem / Directory: / UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- lrwxrwxrwx root root 01/28/2025 18:26 7 bin -> usr/bin dr-xr-xr-x root root 11/15/2025 00:04 882 boot drwxr-xr-x root root 01/07/2026 08:56 3860 dev drwxr-xr-x root root 01/07/2026 08:56 3072 etc dr-xr-xr-x root root 10/09/2025 14:05 8 home ..... ..... # create new file under the current directory PS /root> New-Item -Path test.txt Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- -rw-r--r-- root root 01/07/2026 09:02 0 test.txt PS /root> dir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/28/2025 18:26 0 bin drwx------ root root 01/07/2026 08:59 20 snap -rw-r--r-- root root 01/07/2026 09:02 0 test.txt # create new directory under the current directory PS /root> New-Item -ItemType Directory -Path testdir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/07/2026 09:02 0 testdir PS /root> dir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/28/2025 18:26 0 bin drwx------ root root 01/07/2026 08:59 20 snap drwxr-xr-x root root 01/07/2026 09:02 0 testdir -rw-r--r-- root root 01/07/2026 09:02 0 test.txt # [echo] texts and redirect it to a file PS /root> echo "test content" >> test.txt # display content of a file PS /root> Get-Content test.txt test content # move/rename a file PS /root> Move-Item test.txt test1.txt PS /root> dir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/28/2025 18:26 0 bin drwx------ root root 01/07/2026 08:59 20 snap drwxr-xr-x root root 01/07/2026 09:02 0 testdir -rw-r--r-- root root 01/07/2026 09:03 13 test1.txt # copy a file PS /root> Copy-Item test1.txt test2.txt PS /root> dir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/28/2025 18:26 0 bin drwx------ root root 01/07/2026 08:59 20 snap drwxr-xr-x root root 01/07/2026 09:02 0 testdir -rw-r--r-- root root 01/07/2026 09:03 13 test1.txt -rw-r--r-- root root 01/07/2026 09:03 13 test2.txt # copy a directory recursively PS /root> Copy-Item testdir testdir2 -Recurse PS /root> dir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/28/2025 18:26 0 bin drwx------ root root 01/07/2026 08:59 20 snap drwxr-xr-x root root 01/07/2026 09:02 0 testdir drwxr-xr-x root root 01/07/2026 09:06 0 testdir2 -rw-r--r-- root root 01/07/2026 09:03 13 test1.txt -rw-r--r-- root root 01/07/2026 09:03 13 test2.txt # remove a file PS /root> Remove-Item test2.txt PS /root> dir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/28/2025 18:26 0 bin drwx------ root root 01/07/2026 08:59 20 snap drwxr-xr-x root root 01/07/2026 09:02 0 testdir drwxr-xr-x root root 01/07/2026 09:06 0 testdir2 -rw-r--r-- root root 01/07/2026 09:03 13 test1.txt # remove a directory recursively PS /root> Remove-Item testdir2 -Recurse PS /root> dir Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- drwxr-xr-x root root 01/28/2025 18:26 0 bin drwx------ root root 01/07/2026 08:59 20 snap drwxr-xr-x root root 01/07/2026 09:02 0 testdir -rw-r--r-- root root 01/07/2026 09:03 13 test1.txt # search files which includes [.txt] in thier name under the current directory PS /root> Get-ChildItem "*.txt" -Recurse Directory: /root UnixMode User Group LastWriteTime Size Name -------- ---- ----- ------------- ---- ---- -rw-r--r-- root root 01/07/2026 09:03 13 test1.txt # search a word [test] in a file [test1.txt] PS /root> Select-String -Pattern "test" test1.txt test1.txt:1:test content # show help about a cmdlet PS /root> Get-Help Get-Content NAME Get-Content SYNTAX Get-Content [-Path] <string[]> [-ReadCount <long>] [-TotalCount <long>] [-Tail <int>] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-Delimiter <string>] [-Wait] [-Raw] [-Encoding <Encoding>] [-AsByteStream] [<CommonParameters>] ..... ..... # access to another host with SSH PS /root> ssh serverworld@10.0.0.100 serverworld@10.0.0.100's password: Microsoft Windows [Version 10.0.26100.2314] (c) Microsoft Corporation. All rights reserved. fd3s01\serverworld@FD3S C:\Users\serverworld> dir Volume in drive C has no label. Volume Serial Number is 863E-5E8A Directory of C:\Users\serverworld 01/05/2025 06:02 PM <DIR> . 01/05/2025 06:02 PM <DIR> .. 03/31/2024 11:02 PM <DIR> Desktop 01/05/2025 06:02 PM <DIR> Documents 03/31/2024 11:02 PM <DIR> Downloads 03/31/2024 11:02 PM <DIR> Favorites 03/31/2024 11:02 PM <DIR> Links 03/31/2024 11:02 PM <DIR> Music 03/31/2024 11:02 PM <DIR> Pictures 03/31/2024 11:02 PM <DIR> Saved Games 03/31/2024 11:02 PM <DIR> Videos 0 File(s) 0 bytes 11 Dir(s) 87,845,408,768 bytes free |
|
|