powershell建立並載入配置檔案

2021-09-06 15:45:09 字數 1705 閱讀 6757

$pshome :powershell的主目錄

$profile :顯示 windows powershell 配置檔案的路徑

test-path $profile :確定是否已經在系統上建立了 windows powershell 配置檔案

powershell.exe 主機配置檔案(在 windows vista 中)的位置如下所示:

%windir%\system32\windows­powershell\v1.0\profile.ps1用於計算機的所有使用者和所有外殼。

%windir%\system32\windows­powershell\v1.0\microsoft.power­shell_profile.ps1用於計算機的所有使用者,但僅用於 microsoft.powershell 外殼。

%userprofile%\documents\windows­powershell\profile.ps1僅用於當前使用者和所有外殼。

%userprofile%\documents\windowspowershell\micro­soft.powershell_profile.ps1僅用於當前使用者和 microsoft.powershell 外殼。

啟動時按順序載入,最後乙個優先順序最高,會覆蓋之前的配置檔案

這些配置檔案並不是在預設情況下建立的。必須在您手動建立後,它們才會出現。

例,建立適用於所有使用者和所有 shell 的配置檔案,鍵入:

new-item -path $env:windir\system32\windowspowershell\v1.0\profile.ps1 -itemtype file -force

notepad $env:windir\system32\windowspowershell\v1.0\profile.ps1

如輸入:

c:cd c:\

function pp

編輯後儲存,然後再重新執行powershell.exe,會載入profile.ps1中的內容,在啟動後會自動跳轉到c:路徑下,還會自動載入函式 pp

建立自定義控制台

要建立自定義控制台,首先應查詢要處理的每個管理單元的全名。確保所有必需的管理工具都已安裝在計算機中。然後,在 windows powershell 中執行 get-pssnapin –registered。這將列出所有已註冊但卻未載入的可用管理單元。然後建立或編輯相應的 windows power­shell 配置檔案。新增 add-ps­snapin 命令,載入希望始終可用的每個管理單元。這可能包括用於 exchange server、system center 產品以及第三方管理單元(如 power­shell community extensions)的管理單元。然後儲存配置檔案(請記住,如果 windows power­shell 執行策略需要,則對配置檔案進行數字簽名)並關閉外殼。重新開啟外殼,它會自動載入配置檔案中列出的所有管理單元。

另一種技術是將所有管理單元載入到外殼中(使用 add-pssnapin 和管理單元的名稱),然後執行 export-console 建立乙個 .psc1 控制台檔案,其中包含當前正在使用的所有管理單元。然後,可使用這一 .psc1 控制台檔案建立乙個新的 windows powershell 快捷方式,以指定 psconsole­file 引數和自定義的 .psc1 檔案。該快捷方式隨後會使用您的控制台,並自動載入所有指定的管理單元。

Powershell建立陣列

在powershell中建立陣列可以使用逗號。ps c powershell nums 2,0,1,2 ps c powershell nums20 12對於連續的數字陣列可以使用乙個更快捷的方法 ps c powershell nums 1.5 ps c powershell nums12 345...

powershell載入EXE進記憶體執行

可以使用以下函式 function convert binarytostring catch if bytearray else write output inputobject base64string 執行.base64.ps1 位址 https 在執行之前我需要把base64編碼的字串轉換為字...

Powershell檢測並啟動多個服務

恢復powershell的預設執行策略,預設不允許執行任何指令碼 set executionpolicy default force 設定powershell的執行策略為,可以執行任何指令碼 set executionpolicy unrestricted force 定義乙個傳送郵件的函式send...