Windows powershell 自學指南1


參考MIT課程MIT-Missing-Semester(CS小白入門可學)。

原課程演示用的是Linux系統的操作指令,與我所用不同。

 

Windows powershell 下載安裝略。Microsoft Store 搜索Windows Termianal下載。

Note1:如果需要help指令,以管理員身份運行Windows powershell ,然后輸入指令:Update-help

然后會出現提示:

Update-help : 無法更新帶有 UI 區域性 {zh-CN} 的模塊“ConfigDefender, PSReadline”幫助: 在 HelpInfo XML 文件中檢索不到 U
I 區域性 zh-CN。確保模塊清單中的 HelpInfoUri 屬性有效或檢查網絡連接是否正常,然后重試該命令。
所在位置 行:1 字符: 1
+ Update-help
+ ~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Update-Help], Exception
    + FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand

解決方案:修改操作系統的語言集為英文

或者使用如下代碼替代:

Update-Help -ErrorAction SilentlyContinue

以下所有代碼在Windows PowerShell下運行(不是Command Prompt)

輸入:date  輸出為:

2022年3月30日 22:17:18

 輸入:echo hello   輸出為: hello

Windows PowerShell默認進入的路徑是系統盤的目錄:比如我的是C:\Users\dell>

輸入 :dir 輸出為C:\Users\dell>路徑中的文件夾目錄比如我的是:

 目錄: C:\Users\dell


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2021/5/12     21:02                .astropy
d-----         2021/5/21     16:47                .cache
d-----         2021/5/21     20:39                .matplotlib
d-----         2021/6/20     20:26                .texlive2021
d-----         2021/5/24     18:52                .vscode
d-r---          2022/3/1     19:37                3D Objects
d-----          2021/2/2     14:54                ansel
d-r---         2021/1/29     22:34                Contacts
d-----        2021/11/28     16:13                copytranslator
d-r---         2022/3/30     11:06                Desktop
d-r---          2022/1/9     12:12                Documents
d-r---         2022/3/30     11:00                Downloads
d-r---          2021/2/4     16:59                Favorites
d-----          2021/2/8     16:48                Flash Center
d-r---         2021/10/9     16:36                Links
d-r---         2021/1/29     22:34                Music
d-----         2022/3/23     11:03                mytest
d-r---          2021/8/5     22:49                OneDrive
d-r---         2022/2/18     21:56                Pictures
d-r---         2021/1/29     22:34                Saved Games
d-r---         2021/1/29     22:36                Searches
d-----         2021/11/3     20:33                source
d-----         2021/10/9     16:35                UIDowner
d-r---         2022/3/30     10:43                Videos
-a----         2021/7/18     19:13            672 .condarc
-a----         2021/8/23     16:06            161 BullseyeCoverageError.txt
-a----         2021/5/11     17:19        1937346 get-pip.py
-a----         2022/3/23     10:43              0 test1

 

要查看當前所在路徑,輸入:pwd 輸出:

Path
----
C:\Users\dell

要查看環境變量,輸入:Get-ChildItem env: 輸出:

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\dell\AppData\Roaming
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files
COMPUTERNAME                   DESKTOP-HURPF4U
ComSpec                        C:\Windows\system32\cmd.exe
DriverData                     C:\Windows\System32\Drivers\DriverData
HOMEDRIVE                      C:
HOMEPATH                       \Users\dell
LOCALAPPDATA                   C:\Users\dell\AppData\Local
LOGONSERVER                    \\DESKTOP-HURPF4U
NUMBER_OF_PROCESSORS           12
OneDrive                       C:\Users\dell\OneDrive
OS                             Windows_NT
Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPo...
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
PROCESSOR_ARCHITECTURE         AMD64
PROCESSOR_IDENTIFIER           Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
PROCESSOR_LEVEL                6
PROCESSOR_REVISION             a502
ProgramData                    C:\ProgramData
ProgramFiles                   C:\Program Files
ProgramFiles(x86)              C:\Program Files (x86)
ProgramW6432                   C:\Program Files
PSModulePath                   C:\Users\dell\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\...
PUBLIC                         C:\Users\Public
PyCharm Community Edition      D:\pycharm\PyCharm Community Edition 2021.1.1\bin;
SystemDrive                    C:
SystemRoot                     C:\Windows
TEMP                           C:\Users\dell\AppData\Local\Temp
TMP                            C:\Users\dell\AppData\Local\Temp
USERDOMAIN                     DESKTOP-HURPF4U
USERDOMAIN_ROAMINGPROFILE      DESKTOP-HURPF4U
USERNAME                       dell
USERPROFILE                    C:\Users\dell
windir                         C:\Windows
WSLENV                         WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID                  {61c54bbd-c2c6-5271-96e7-009a87ff44bf}
WT_SESSION                     d7061c74-806b-4c75-8a42-bfcf23353b2d
ZES_ENABLE_SYSMAN              1

 

 

要查找文件或者程序的位置(比如搜索matlab程序的路徑),輸入:gcm MATLAB 輸出:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     matlab.exe                                         1.0.0.1    E:\MATLAB\bin\matlab.exe

gcm在當前目錄和上述環境變量中搜索

 

要返回上一級路徑,現在是C:\Users\dell,我們想返回C:\Users

輸入: cd..(2個點)  那么路徑變成C:\Users

此時輸入:pwd 查看當前路徑,輸出:

Path
----
C:\Users

 

要更改路徑,比如進入D盤,輸入:D:或者(cd D:)這路徑變為:D:\>

要進入某個文件夾,比如我D盤里有個文件夾叫skr,輸入:cd skr 這時路徑變為:D:\skr>

返回默認路徑,輸入:cd ~ 這時路徑變為: C:\Users\dell>

查看某條指令的幫助 輸入 :help 某條指令 比如  help cd 輸出:

名稱
    Set-Location

摘要
    Sets the current working location to a specified location.


語法
    Set-Location -LiteralPath <System.String> [-PassThru] [-UseTransaction] [<CommonParameters>]

    Set-Location [[-Path] <System.String>] [-PassThru] [-UseTransaction] [<CommonParameters>]

    Set-Location [-PassThru] [-StackName <System.String>] [-UseTransaction] [<CommonParameters>]


說明
    The `Set-Location` cmdlet sets the working location to a specified location. That location could be a directory, a
    subdirectory, a registry location, or any provider path.

    You can also use the StackName parameter to make a named location stack the current location stack. For more inform
    ation about location stacks, see the Notes.


相關鏈接
    Online Version: https://docs.microsoft.com/powershell/module/microsoft.powershell.management/set-location?view=powe
    rshell-5.1&WT.mc_id=ps-gethelp
    Get-Location
    Pop-Location

 

需要清空當前並返回首行,輸入:clear

在某個路徑中新建一個文件夾,比如在D盤里建立一個名叫skr2的文件夾(路徑)(確保D盤里事先沒有skr2)

先進入D盤路徑 輸入: D:

再輸入: ni skr2 -type dir 輸出:

   
    目錄: D:\


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2022/3/30     22:44                skr2

 

比如在skr2里新建一個demo.txt文檔

進入skr2 路徑 cd skr2

再輸入:ni demo.txt -type file 輸出:
    目錄: D:\skr2


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2022/3/30     22:48              0 demo.txt

在demo.txt文檔里寫入內容

輸入: sc demo.txt "say hello to my little friends"

則demo.txt 的文檔的第一行為:say hello to my little friends

sc指令會覆蓋之前的內容

比如想再上述文檔中追加新的內容

輸入: ac demo.txt hello

則demo.txt 的文檔的第二行為:hello

輸出文檔內容,輸入:type demo.txt 輸出:

say hello to my little friends
hello

 

移動demo.txt到demo1.txt (確保事先沒有demo1.txt文件)輸入: mv demo.txt demo1.txt

復制demo1.txt到demo2.txt (確保事先沒有demo2.txt文件)輸入: cp demo1.txt demo2.txt

刪除demo.txt 輸入:rm demo.txt

將所有的demo文件刪除后才可以將路徑skr2刪除

返回D盤路徑,輸入:cd ..

然后輸入:rm skr2
可以將skr2文件夾刪除。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM