Debian 12 bookworm
Sponsored Link

PowerShell : Install2023/07/17

 
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.3.6 from Microsoft PowerShell✓ installed
[3] This is the Basic usage of PowerShell.
# run PowerShell

root@dlp:~#
pwsh

PowerShell 7.3.6

PS /root>

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

CommandType Name                Version Source
----------- ----                ------- ------
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
Function    Find-Module         2.2.5   PowerShellGet
Function    Find-RoleCapability 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 directory (dir equals Get-ChildItem)
PS /root> dir | ft -AutoSize -Wrap 

    Directory: /root

UnixMode   User             Group                 LastWriteTime           Size
--------   ----             -----                 -------------           ----
drwx------ root             root                7/17/2023 01:00           4096
-rw-r--r-- root             root                7/17/2023 00:40        3115940

# display files under /
PS /root> Get-ChildItem / | ft -AutoSize -Wrap 

    Directory: /

UnixMode   User Group   LastWriteTime  Size Name
--------   ---- -----   -------------  ---- ----
lrwxrwxrwx root root  6/11/2023 19:27     7 bin -> usr/bin
drwxr-xr-x root root  6/11/2023 19:31  1024 boot
drwxr-xr-x root root  7/17/2023 00:59  3320 dev
drwxr-xr-x root root  7/17/2023 00:58  4096 etc
drwxr-xr-x root root  6/11/2023 19:31  4096 home
.....
.....

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

    Directory: /root

UnixMode   User             Group                 LastWriteTime           Size
--------   ----             -----                 -------------           ----
-rw-r--r-- root             root                7/17/2023 01:05              0

PS /root> dir | ft -AutoSize -Wrap 

    Directory: /root

UnixMode   User Group   LastWriteTime    Size Name
--------   ---- -----   -------------    ---- ----
drwx------ root root  7/17/2023 01:00    4096 snap
-rw-r--r-- root root  7/17/2023 00:40 3115940 redmine-5.0.5.tar.gz
-rw-r--r-- root root  7/17/2023 01:05       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
--------   ----             -----                 -------------           ----
drwxr-xr-x root             root                7/17/2023 01:06           4096

PS /root> dir | ft -AutoSize -Wrap 

    Directory: /root

UnixMode   User Group   LastWriteTime    Size Name
--------   ---- -----   -------------    ---- ----
drwx------ root root  7/17/2023 01:00    4096 snap
drwxr-xr-x root root  7/17/2023 01:06    4096 testdir
-rw-r--r-- root root  7/17/2023 00:40 3115940 redmine-5.0.5.tar.gz
-rw-r--r-- root root  7/17/2023 01:05       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 | ft -AutoSize -Wrap 

    Directory: /root

UnixMode   User Group   LastWriteTime    Size Name
--------   ---- -----   -------------    ---- ----
drwx------ root root  7/17/2023 01:00    4096 snap
drwxr-xr-x root root  7/17/2023 01:06    4096 testdir
-rw-r--r-- root root  7/17/2023 00:40 3115940 redmine-5.0.5.tar.gz
-rw-r--r-- root root  7/17/2023 01:07      13 test1.txt

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

    Directory: /root

UnixMode   User Group   LastWriteTime    Size Name
--------   ---- -----   -------------    ---- ----
drwx------ root root  7/17/2023 01:00    4096 snap
drwxr-xr-x root root  7/17/2023 01:06    4096 testdir
-rw-r--r-- root root  7/17/2023 00:40 3115940 redmine-5.0.5.tar.gz
-rw-r--r-- root root  7/17/2023 01:07      13 test1.txt
-rw-r--r-- root root  7/17/2023 01:07      13 test2.txt

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

    Directory: /root

UnixMode   User Group   LastWriteTime    Size Name
--------   ---- -----   -------------    ---- ----
drwx------ root root  7/17/2023 01:00    4096 snap
drwxr-xr-x root root  7/17/2023 01:06    4096 testdir
drwxr-xr-x root root  7/17/2023 01:08    4096 testdir2
-rw-r--r-- root root  7/17/2023 00:40 3115940 redmine-5.0.5.tar.gz
-rw-r--r-- root root  7/17/2023 01:07      13 test1.txt
-rw-r--r-- root root  7/17/2023 01:07      13 test2.txt

# remove a file
PS /root> Remove-Item test2.txt 
PS /root> dir | ft -AutoSize -Wrap 

    Directory: /root

UnixMode   User Group   LastWriteTime    Size Name
--------   ---- -----   -------------    ---- ----
drwx------ root root  7/17/2023 01:00    4096 snap
drwxr-xr-x root root  7/17/2023 01:06    4096 testdir
drwxr-xr-x root root  7/17/2023 01:08    4096 testdir2
-rw-r--r-- root root  7/17/2023 00:40 3115940 redmine-5.0.5.tar.gz
-rw-r--r-- root root  7/17/2023 01:07      13 test1.txt

# remove a directory recursively
PS /root> Remove-Item testdir2 -Recurse 
PS /root> dir | ft -AutoSize -Wrap 

    Directory: /root

UnixMode   User Group   LastWriteTime    Size Name
--------   ---- -----   -------------    ---- ----
drwx------ root root  7/17/2023 01:00    4096 snap
drwxr-xr-x root root  7/17/2023 01:06    4096 testdir
-rw-r--r-- root root  7/17/2023 00:40 3115940 redmine-5.0.5.tar.gz
-rw-r--r-- root root  7/17/2023 01:07      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
--------   ----             -----                 -------------           ----
-rw-r--r-- root             root                7/17/2023 01:07             13

# 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