CentOS 7
Sponsored Link

Install PowerShell for Linux2016/09/27

 
Install Microsoft PowerShell for Linux.
Refer to the details about PowerShell for Linux below.
  ⇒ https://github.com/PowerShell/PowerShell/blob/master/docs/installation/linux.md
[1] Install PowerShell.
[root@dlp ~]#
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/microsoft.repo

[root@dlp ~]#
yum -y install powershell
[2] Basic operation of PowerShell.
# run PowerShell

[root@dlp ~]#
pwsh

PowerShell v6.0.0-rc
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    Source
-----------     ----                                               -------    ------
Function        Add-NodeKeys                                       0.0        PSDesiredStateConfiguration
Function        AddDscResourceProperty                             0.0        PSDesiredStateConfiguration
Function        AddDscResourcePropertyFromMetadata                 0.0        PSDesiredStateConfiguration
Function        AfterAll                                           3.3.9      Pester
Function        AfterEach                                          3.3.9      Pester
Function        Assert-MockCalled                                  3.3.9      Pester
Function        Assert-VerifiableMocks                             3.3.9      Pester
Function        BeforeAll                                          3.3.9      Pester
Function        BeforeEach                                         3.3.9      Pester
Function        cd..

# 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
----                -------------         ------ ----
------           1/8/15   7:52 AM            985 anaconda-ks.cfg

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

    Directory: /

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d----l         12/17/15   7:05 PM                bin
d-r---          9/23/16  11:32 PM                boot
d-----          9/28/16   7:27 PM                dev
.....
.....

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

    Directory: /root

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          9/28/16   8:52 PM              0 test.txt

PS /root> dir 

    Directory: /root

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------           1/8/15   7:52 AM            985 anaconda-ks.cfg
------          9/28/16   8:52 PM              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/28/16   8:55 PM                testdir

PS /root> dir 

    Directory: /root

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----          9/28/16   8:55 PM                testdir
------           1/8/15   7:52 AM            985 anaconda-ks.cfg
------          9/28/16   8:52 PM              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/28/16   8:55 PM                testdir
------           1/8/15   7:52 AM            985 anaconda-ks.cfg
------          9/28/16   8:57 PM             28 test1.txt

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

    Directory: /root

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----          9/28/16   8:55 PM                testdir
------           1/8/15   7:52 AM            985 anaconda-ks.cfg
------          9/28/16   8:57 PM             28 test1.txt
------          9/28/16   8:57 PM             28 test2.txt

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

    Directory: /root

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----          9/28/16   9:04 PM                testdir
d-----          9/28/16   9:04 PM                testdir2
------           1/8/15   7:52 AM            985 anaconda-ks.cfg
------          9/28/16   8:57 PM             28 test1.txt
------          9/28/16   8:57 PM             28 test2.txt

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

    Directory: /root

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----          9/28/16   9:04 PM                testdir
d-----          9/28/16   9:04 PM                testdir2
------           1/8/15   7:52 AM            985 anaconda-ks.cfg
------          9/28/16   8:57 PM             28 test1.txt

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

    Directory: /root


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----          9/28/16   9:04 PM                testdir
------           1/8/15   7:52 AM            985 anaconda-ks.cfg
------          9/28/16   8:57 PM             28 test1.txt

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

    Directory: /root/testdir


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          9/28/16   8:57 PM             28 test3.txt

    Directory: /root


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          9/28/16   8:57 PM             28 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

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


SYNTAX
    Get-Content [-Path] <String[]> [-Credential <PSCredential>] [-Delimiter <System.String>] 
    [-Encoding {Unknown | String | Unicode | Byte
.....
.....

# 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