Debian 11 Bullseye
Sponsored Link

PowerShell : Install2021/09/29

 
Install Microsoft PowerShell for Linux.
Refer to the details about PowerShell for Linux below.
  ⇒ https://github.com/PowerShell/PowerShell
[1]
[2] Install Microsoft PowerShell from Snappy.
root@dlp:~#
snap install powershell --classic

powershell 7.1.4 from Microsoft PowerShell✓ installed
[3] This is the Basic usage of PowerShell.
# run PowerShell

root@dlp:~#
pwsh

PowerShell 7.1.4
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /root>

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

CommandType     Name                                               Version    S
-----------     ----                                               -------    -
Function        cd..
Function        cd\
Function        Clear-Host
Function        Compress-Archive                                   1.2.5      M
Function        Configuration                                      2.0.5      P
Function        Expand-Archive                                     1.2.5      M
Function        Find-Command                                       2.2.5      P
Function        Find-DSCResource                                   2.2.5      P
Function        Find-Module                                        2.2.5      P
Function        Find-RoleCapability                                2.2.5      P

# 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

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:00 AM                snap
d----           9/29/2021 11:04 AM                testdir
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 10:43 AM           5933 script.deb.sh

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

    Directory: /

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
l----           8/16/2021  3:16 PM                bin -> /usr/bin
d----           8/16/2021  3:27 PM                boot
d----           9/29/2021  9:36 AM                dev
d----           9/29/2021 10:45 AM                etc
d----           9/29/2021 10:21 AM                home
.....
.....

# create new file under the current directory
PS /root> New-Item -Path test.txt 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----           9/29/2021 11:03 AM              0 test.txt

PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:00 AM                snap
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 10:43 AM           5933 script.deb.sh
-----           9/29/2021 11:03 AM              0 test.txt

# create new directory under the current directory
PS /root> New-Item -ItemType Directory -Path testdir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:13 AM                testdir

PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:00 AM                snap
d----           9/29/2021 11:13 AM                testdir
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 10:43 AM           5933 script.deb.sh
-----           9/29/2021 11:03 AM              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

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:00 AM                snap
d----           9/29/2021 11:13 AM                testdir
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 10:43 AM           5933 script.deb.sh
-----           9/29/2021 11:14 AM             13 test1.txt

# copy a file
PS /root> Copy-Item test1.txt test2.txt 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:00 AM                snap
d----           9/29/2021 11:13 AM                testdir
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 10:43 AM           5933 script.deb.sh
-----           9/29/2021 11:14 AM             13 test1.txt
-----           9/29/2021 11:14 AM             13 test2.txt

# copy a directory recursively
PS /root> Copy-Item testdir testdir2 -Recurse 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:00 AM                snap
d----           9/29/2021 11:13 AM                testdir
d----           9/29/2021 11:14 AM                testdir2
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 10:43 AM           5933 script.deb.sh
-----           9/29/2021 11:14 AM             13 test1.txt
-----           9/29/2021 11:14 AM             13 test2.txt

# remove a file
PS /root> Remove-Item test2.txt 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:00 AM                snap
d----           9/29/2021 11:13 AM                testdir
d----           9/29/2021 11:14 AM                testdir2
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 10:43 AM           5933 script.deb.sh
-----           9/29/2021 11:14 AM             13 test1.txt

# remove a directory recursively
PS /root> Remove-Item testdir2 -Recurse 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           9/29/2021 11:00 AM                snap
d----           9/29/2021 11:13 AM                testdir
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 10:43 AM           5933 script.deb.sh
-----           9/29/2021 11:14 AM             13 test1.txt

# search files which includes [.txt] in thier name under the current directory
PS /root> Get-ChildItem "*.txt" -Recurse 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----           9/29/2021 10:39 AM            218 acl.txt
-----           9/29/2021 11:14 AM             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>]
.....
.....
Matched Content