CentOS Stream 8
Sponsored Link

PowerShell for Linux : インストール2021/04/14

 
Microsoft の PowerShell for Linux をインストールします。
概要は公式サイトを参照ください。
  ⇒ https://github.com/PowerShell/PowerShell
[1] PowerShell をインストールします。
[root@dlp ~]#
curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/microsoft.repo

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

[2] PowerShell の起動と主な操作です。
# PowerShell 起動

[root@dlp ~]#
pwsh

PowerShell 7.1.3
Copyright (c) Microsoft Corporation.

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

PS /root>

# コマンドレット一覧表示 (下例は頭 10 行のみ表示)
# 全て表示する場合は単に Get-Command で OK
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        Configuration                                      2.0.5      PSDesiredStateConfiguration
Function        Expand-Archive                                     1.2.5      Microsoft.PowerShell.Archive
Function        Find-Command                                       2.2.4.1    PowerShellGet
Function        Find-DSCResource                                   2.2.4.1    PowerShellGet
Function        Find-Module                                        2.2.4.1    PowerShellGet
Function        Find-RoleCapability                                2.2.4.1    PowerShellGet

# カレントPATH 表示
PS /root> pwd 

Path
----
/root

# /home へ移動
PS /root> cd /home 

# 引数なしでホームディレクトリに戻る
PS /home> cd 

# カレントディレクトリのファイル一覧を表示 (Get-ChildItem でも dir と同じ)
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----            3/9/2021 12:14 AM           1535 anaconda-ks.cfg
-----            3/9/2021 12:24 AM           1826 initial-setup-ks.cfg

# / のファイル一覧を表示
PS /root> Get-ChildItem / 

    Directory: /

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
l----           5/18/2020  2:40 PM                bin -> /usr/bin
d-r--            3/9/2021 12:13 AM                boot
d----           4/13/2021 10:58 PM                dev
d----           4/13/2021 10:58 PM                etc
d----            3/9/2021 12:31 AM                home
.....
.....

# カレントディレクトリにファイル新規作成
PS /root> New-Item -Path test.txt 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----           4/13/2021 11:10 PM              0 test.txt

PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----            3/9/2021 12:14 AM           1535 anaconda-ks.cfg
-----            3/9/2021 12:24 AM           1826 initial-setup-ks.cfg
-----           4/13/2021 11:10 PM              0 test.txt

# カレントディレクトリにディレクトリ新規作成
PS /root> New-Item -ItemType Directory -Path testdir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           4/13/2021 11:14 PM                testdir

PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
d----           4/13/2021 11:14 PM                testdir
-----            3/9/2021 12:14 AM           1535 anaconda-ks.cfg
-----            3/9/2021 12:24 AM           1826 initial-setup-ks.cfg
-----           4/13/2021 11:10 PM              0 test.txt

# echo してファイルにリダイレクト
PS /root> echo "test content" >> test.txt 

# ファイルの内容を表示
PS /root> Get-Content test.txt 
test content

# ファイルを移動/リネームする
PS /root> Move-Item test.txt test1.txt 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           4/13/2021 11:14 PM                testdir
-----            3/9/2021 12:14 AM           1535 anaconda-ks.cfg
-----            3/9/2021 12:24 AM           1826 initial-setup-ks.cfg
-----           4/13/2021 11:14 PM             13 test1.txt

# ファイルをコピーする
PS /root> Copy-Item test1.txt test2.txt 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           4/13/2021 11:14 PM                testdir
-----            3/9/2021 12:14 AM           1535 anaconda-ks.cfg
-----            3/9/2021 12:24 AM           1826 initial-setup-ks.cfg
-----           4/13/2021 11:14 PM             13 test1.txt
-----           4/13/2021 11:14 PM             13 test2.txt

# ディレクトリを中身も含めて再帰的にコピーする
PS /root> Copy-Item testdir testdir2 -Recurse 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           4/13/2021 11:14 PM                testdir
d----           4/13/2021 11:16 PM                testdir2
-----            3/9/2021 12:14 AM           1535 anaconda-ks.cfg
-----            3/9/2021 12:24 AM           1826 initial-setup-ks.cfg
-----           4/13/2021 11:14 PM             13 test1.txt
-----           4/13/2021 11:14 PM             13 test2.txt

# ファイルを削除する
PS /root> Remove-Item test2.txt 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           4/13/2021 11:14 PM                testdir
d----           4/13/2021 11:16 PM                testdir2
-----            3/9/2021 12:14 AM           1535 anaconda-ks.cfg
-----            3/9/2021 12:24 AM           1826 initial-setup-ks.cfg
-----           4/13/2021 11:14 PM             13 test1.txt

# ディレクトリを中身を含めて削除する
PS /root> Remove-Item testdir2 -Recurse 
PS /root> dir 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           4/13/2021 11:14 PM                testdir
-----            3/9/2021 12:14 AM           1535 anaconda-ks.cfg
-----            3/9/2021 12:24 AM           1826 initial-setup-ks.cfg
-----           4/13/2021 11:14 PM             13 test1.txt

# カレントディレクトリ配下から拡張子 .txt のファイルを検索する
PS /root> Get-ChildItem "*.txt" -Recurse 

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----           4/13/2021 11:14 PM             13 test1.txt


# test1.txt から "test" 文字列を検索する
PS /root> Select-String -Pattern "test" test1.txt 

test1.txt:1:test content

# 引数に指定したコマンドレットのヘルプを表示する
PS /root> Get-Help Get-Content 

NAME
    Get-Content

SYNOPSIS
    Gets the content of the item at the specified location.


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>]
.....
.....

# 他ホストに 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
関連コンテンツ