CentOS Stream 10
Sponsored Link

PowerShell for Linux : インストール2025/01/06

 

Microsoft の PowerShell for Linux をインストールします。
概要は公式サイトを参照ください。
  ⇒ https://github.com/PowerShell/PowerShell

[1]

こちらを参考に Snapd をインストールしておきます

[2] PowerShell をインストールします。
[root@dlp ~]#
snap install powershell --classic

powershell 7.4.6 from Microsoft PowerShell✓ installed
[3] PowerShell の起動と主な操作です。
# PowerShell 起動

[root@dlp ~]#
pwsh

PowerShell 7.4.6
PS /root>

# コマンドレット一覧表示 (下例は頭 10 行のみ表示)
# 全て表示する場合は単に Get-Command で OK
PS /root> (Get-Command)[0..9] | Format-Table -AutoSize 

CommandType Name             Version Source
----------- ----             ------- ------
Alias       Get-PSResource   1.0.4.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

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

Path
----
/root

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

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

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

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwx------       root root         1/6/2025 10:45           24 snap
-rw-------       root root       12/14/2024 18:35          981 anaconda-ks.cfg

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

    Directory: /

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
dr-xr-xr-x       root root       10/29/2024 09:00            6 afs
lrwxrwxrwx       root root       10/29/2024 09:00            7 bin -> usr/bin
dr-xr-xr-x       root root         1/6/2025 10:14         4096 boot
drwxr-xr-x       root root         1/6/2025 10:38         3280 dev
drwxr-xr-x       root root         1/6/2025 10:37         8192 etc
drwxr-xr-x       root root       12/14/2024 18:35           18 home
lrwxrwxrwx       root root       10/29/2024 09:00            7 lib -> usr/lib
.....
.....

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

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
-rw-r--r--       root root         1/6/2025 10:50            0 test.txt

PS /root> dir 

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwx------       root root         1/6/2025 10:45           24 snap
-rw-------       root root       12/14/2024 18:35          981 anaconda-ks.cfg
-rw-r--r--       root root         1/6/2025 10:50            0 test.txt

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

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwxr-xr-x       root root         1/6/2025 10:50            6 testdir

PS /root> dir 

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwx------       root root         1/6/2025 10:45           24 snap
drwxr-xr-x       root root         1/6/2025 10:50            6 testdir
-rw-------       root root       12/14/2024 18:35          981 anaconda-ks.cfg
-rw-r--r--       root root         1/6/2025 10:50            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

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwx------       root root         1/6/2025 10:45           24 snap
drwxr-xr-x       root root         1/6/2025 10:50            6 testdir
-rw-------       root root       12/14/2024 18:35          981 anaconda-ks.cfg
-rw-r--r--       root root         1/6/2025 10:51           13 test1.txt

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

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwx------       root root         1/6/2025 10:45           24 snap
drwxr-xr-x       root root         1/6/2025 10:50            6 testdir
-rw-------       root root       12/14/2024 18:35          981 anaconda-ks.cfg
-rw-r--r--       root root         1/6/2025 10:51           13 test1.txt
-rw-r--r--       root root         1/6/2025 10:51           13 test2.txt

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

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwx------       root root         1/6/2025 10:45           24 snap
drwxr-xr-x       root root         1/6/2025 10:50            6 testdir
drwxr-xr-x       root root         1/6/2025 10:52            6 testdir2
-rw-------       root root       12/14/2024 18:35          981 anaconda-ks.cfg
-rw-r--r--       root root         1/6/2025 10:51           13 test1.txt
-rw-r--r--       root root         1/6/2025 10:51           13 test2.txt

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

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwx------       root root         1/6/2025 10:45           24 snap
drwxr-xr-x       root root         1/6/2025 10:50            6 testdir
drwxr-xr-x       root root         1/6/2025 10:52            6 testdir2
-rw-------       root root       12/14/2024 18:35          981 anaconda-ks.cfg
-rw-r--r--       root root         1/6/2025 10:51           13 test1.txt

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

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwx------       root root         1/6/2025 10:45           24 snap
drwxr-xr-x       root root         1/6/2025 10:50            6 testdir
-rw-------       root root       12/14/2024 18:35          981 anaconda-ks.cfg
-rw-r--r--       root root         1/6/2025 10:51           13 test1.txt

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

    Directory: /root

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
-rw-r--r--       root root         1/6/2025 10:51           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

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 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
関連コンテンツ