CentOS Stream 9
Sponsored Link

PowerShell for Linux : Install2023/12/06

 

Install Microsoft PowerShell for Linux.
Refer to the details about PowerShell for Linux below.
  ⇒ https://github.com/PowerShell/PowerShell

[1] Install Microsoft PowerShell.
[root@dlp ~]#
curl https://packages.microsoft.com/config/rhel/9/prod.repo > /etc/yum.repos.d/microsoft.repo

[root@dlp ~]#
dnf -y install powershell

[2] This is the Bbsic usage of PowerShell.
# run PowerShell

[root@dlp ~]#
pwsh

PowerShell 7.4.0
PS /root>

# display Cmdlet list (only display 10 lines from the head)
PS /root> (Get-Command)[0..9] 

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           Get-PSResource                                     1.0.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
--------         ---- -----         -------------         ---- ----
-rw-------       root root        9/19/2022 11:10         1024 anaconda-ks.cfg

# display files under /
PS /root> Get-ChildItem / 

    Directory: /

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
dr-xr-xr-x       root root        8/10/2021 05:40            6 afs
lrwxrwxrwx       root root        8/10/2021 05:40            7 bin -> usr/bin
dr-xr-xr-x       root root       10/13/2023 08:39         4096 boot
drwxr-xr-x       root root        12/6/2023 09:54         3180 dev
drwxr-xr-x       root root        12/6/2023 09:55         8192 etc
drwxr-xr-x       root root        9/19/2022 11:09           18 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        12/6/2023 09:58            0 test.txt

PS /root> dir 

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
-rw-------       root root        9/19/2022 11:10         1024 anaconda-ks.cfg
-rw-r--r--       root root        12/6/2023 09:58            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        12/6/2023 09:59            6 testdir

PS /root> dir 

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwxr-xr-x       root root        12/6/2023 09:59            6 testdir
-rw-------       root root        9/19/2022 11:10         1024 anaconda-ks.cfg
-rw-r--r--       root root        12/6/2023 09:58            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        12/6/2023 09:59            6 testdir
-rw-------       root root        9/19/2022 11:10         1024 anaconda-ks.cfg
-rw-r--r--       root root        12/6/2023 10:00           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        12/6/2023 09:59            6 testdir
-rw-------       root root        9/19/2022 11:10         1024 anaconda-ks.cfg
-rw-r--r--       root root        12/6/2023 10:00           13 test1.txt
-rw-r--r--       root root        12/6/2023 10:00           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        12/6/2023 09:59            6 testdir
drwxr-xr-x       root root        12/6/2023 10:01            6 testdir2
-rw-------       root root        9/19/2022 11:10         1024 anaconda-ks.cfg
-rw-r--r--       root root        12/6/2023 10:00           13 test1.txt
-rw-r--r--       root root        12/6/2023 10:00           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        12/6/2023 09:59            6 testdir
drwxr-xr-x       root root        12/6/2023 10:01            6 testdir2
-rw-------       root root        9/19/2022 11:10         1024 anaconda-ks.cfg
-rw-r--r--       root root        12/6/2023 10:00           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        12/6/2023 09:59            6 testdir
-rw-------       root root        9/19/2022 11:10         1024 anaconda-ks.cfg
-rw-r--r--       root root        12/6/2023 10:00           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        12/6/2023 10:00           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 winuser@10.0.0.220 
winuser@10.0.0.220's password:
Microsoft Windows [Version 10.0.17763.1158]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\winuser> dir 
 Volume in drive C has no label.
 Volume Serial Number is D4E4-BE4E

 Directory of C:\Users\winuser

2016/09/28  21:42    <DIR>          .
2016/09/28  21:42    <DIR>          ..
2016/09/28  21:50    <DIR>          .ssh
2020/07/20  11:51    <DIR>          3D Objects
2020/07/20  11:51    <DIR>          Contacts
2020/07/20  11:51    <DIR>          Desktop
2020/07/20  11:51    <DIR>          Documents
2020/07/20  11:51    <DIR>          Downloads
2020/07/20  11:51    <DIR>          Favorites
2020/07/20  11:51    <DIR>          Links
2020/07/20  11:51    <DIR>          Music
2020/07/20  11:51    <DIR>          Pictures
2020/07/20  11:51    <DIR>          Saved Games
2020/07/20  11:51    <DIR>          Searches
2020/07/20  11:51    <DIR>          Videos

               0 File(s)              0 bytes
              15 Dir(s)  172,000,489,472 bytes free
Matched Content