Ubuntu 18.04
Sponsored Link

Install PowerShell for Linux
2018/11/30
 
Install Microsoft PowerShell for Linux.
Refer to the details about PowerShell for Linux below.
  ⇒ https://github.com/PowerShell/PowerShell
[1] Install PowerShell.
root@dlp:~#
curl -O https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

root@dlp:~#
dpkg -i packages-microsoft-prod.deb

root@dlp:~#
apt update

root@dlp:~#
apt -y install powershell
[2] This is the basic usage of PowerShell.
# run PowerShell

root@dlp:~#
pwsh

PowerShell 6.1.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
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
                                                                              o
                                                                              u
                                                                              r
                                                                              c
                                                                              e
-----------     ----                                               -------    -
Function        Add-NodeKeys                                       0.0        P
Function        AddDscResourceProperty                             0.0        P
Function        AddDscResourcePropertyFromMetadata                 0.0        P
Function        cd..
Function        cd\
Function        CheckResourceFound                                 0.0        P
Function        Clear-Host
Function        Compress-Archive                                   1.2.2.0    M
Function        Configuration                                      0.0        P
Function        ConvertTo-MOFInstance                              0.0        P

# display 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
----                -------------         ------ ----
------         11/30/18  10:25 AM           2644 packages-microsoft-prod.deb

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

    Directory: /


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         11/26/18  10:57 AM                bin
d-----         11/26/18  11:00 AM                boot
d-----         11/30/18  10:22 AM                dev
d-----         11/30/18  10:25 AM                etc
d-----          4/27/18  10:47 AM                home
d-----          4/27/18  10:42 AM                lib
d-----          4/27/18  10:28 AM                lib64
.....
.....

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

    Directory: /root


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------         11/30/18  10:32 AM              0 test.txt

PS /root> dir 

    Directory: /root


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------         11/30/18  10:25 AM           2644 packages-microsoft-prod.deb
------         11/30/18  10:32 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-----         11/30/18  10:33 AM                testdir


PS /root> dir 

    Directory: /root


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         11/30/18  10:33 AM                testdir
------         11/30/18  10:25 AM           2644 packages-microsoft-prod.deb
------         11/30/18  10:32 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-----         11/30/18  10:33 AM                testdir
------         11/30/18  10:25 AM           2644 packages-microsoft-prod.deb
------         11/30/18  10:34 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-----         11/30/18  10:33 AM                testdir
------         11/30/18  10:25 AM           2644 packages-microsoft-prod.deb
------         11/30/18  10:34 AM             13 test1.txt
------         11/30/18  10:34 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-----         11/30/18  10:33 AM                testdir
d-----         11/30/18  10:35 AM                testdir2
------         11/30/18  10:25 AM           2644 packages-microsoft-prod.deb
------         11/30/18  10:34 AM             13 test1.txt
------         11/30/18  10:34 AM             13 test2.txt

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

    Directory: /root


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         11/30/18  10:33 AM                testdir
d-----         11/30/18  10:35 AM                testdir2
------         11/30/18  10:25 AM           2644 packages-microsoft-prod.deb
------         11/30/18  10:34 AM             13 test1.txt

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

    Directory: /root


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         11/30/18  10:33 AM                testdir
------         11/30/18  10:25 AM           2644 packages-microsoft-prod.deb
------         11/30/18  10:34 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
----                -------------         ------ ----
------         11/30/18  10:34 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>]
.....
.....

# 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.14393]
(c) 2016 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
2016/09/24  01:30    <DIR>          Contacts
2016/09/28  21:37    <DIR>          Desktop
2016/09/24  01:30    <DIR>          Documents
2016/09/24  01:30    <DIR>          Downloads
2016/09/24  01:30    <DIR>          Favorites
2016/09/24  01:30    <DIR>          Links
2016/09/24  01:30    <DIR>          Music
2016/09/25  00:44    <DIR>          OneDrive
2016/09/24  01:30    <DIR>          Pictures
2016/09/24  01:30    <DIR>          Saved Games
2016/09/24  01:30    <DIR>          Searches
2016/09/24  01:30    <DIR>          Videos
               0 File(s)              0 bytes
              15 Dir(s)  172,000,489,472 bytes free
Matched Content
 
Tweet