$pshome :powershell的主目錄html
$profile :顯示 Windows PowerShell 配置文件的路徑web
test-path $profile :肯定是否已經在系統上建立了 Windows PowerShell 配置文件算法
使用如圖所示:shell
powershell.exe 主機配置文件(在 Windows Vista 中)的位置以下所示:數組
%windir%\system32\WindowsPowerShell\v1.0\profile.ps1用於計算機的全部用戶和全部外殼。函數
%windir%\system32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 用於計算機的全部用戶,但僅用於 Microsoft.PowerShell 外殼。post
%UserProfile%\Documents\WindowsPowerShell\profile.ps1僅用於當前用戶和全部外殼。測試
%UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1僅用於當前用戶和 Microsoft.PowerShell 外殼。spa
啓動時按順序加載,最後一個優先級最高,會覆蓋以前的配置文件 這些配置文件並非在默認狀況下建立的。必須在您手動建立後,它們纔會出現。任何建立文本文件的方式,在這裏都適用。但請注意文件的擴展名必須是.ps1。簡單起見,咱們使用命令類建立,建立適用於全部用戶和全部 shell 的配置文件,鍵入:.net
new-item -path $env:windir\System32\WindowsPowerShell\v1.0\profile.ps1 -itemtype file -force
便在所示目錄下建立了配置文件,而後使用notepad(若是安裝的話)編輯文件:
notepad $env:windir\System32\WindowsPowerShell\v1.0\profile.ps1
輸入以下內容:
d: function pp { write-host "ppc" }
編輯後保存,而後再從新運行powershell.exe,會加載profile.ps1中的內容,啓動後若是自動跳轉到D:路徑下,說明文件已加載,可進一步測試 pp函數。
若是出現PowerShell 默認不容許執行*.ps1腳本文件。以下圖所示:
能夠經過Get-ExecutionPolicy,來取得當前策略。用Set-ExecutionPolicy設置當前策略。下面的命令能夠解決上面的錯誤
PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned <按回車> Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution policy? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):<按Y>
Policy的有效參數:
-- Restricted: 不載入任何配置文件,不運行任何腳本。 "Restricted" 是默認的。
-- AllSigned: 只有被Trusted publisher簽名的腳本或者配置文件才能使用,包括你本身再本地寫的腳本
-- RemoteSigned: 對於從Internet上下載的腳本或者配置文件,只有被Trusted publisher簽名的才能使用。
-- Unrestricted: 能夠載入全部配置文件,能夠運行全部腳本文件. 若是你運行一個從internet下載而且沒有簽名的腳本,在運行以前,你會被提示須要必定的權限。
-- Bypass: 全部東西均可以使用,而且沒有提示和警告.
-- Undefined: 刪除當前scope被賦予的Execution Policy. 可是Group Policy scope的Execution Policy不會被刪除.
須要注意的是使用Notepad++編輯文件會有問題,能夠用Windows自帶的notepad編輯,建議使用ps1文件右鍵中的編輯菜單調用powershell_ise.exe進行編輯。
如圖所示:
編輯完成後可直接點擊運行按鈕來測試腳本文件。
function wac ($x){ if($x.StartsWith(「.」)){ $ip='192.168'+$x write-host 'adb connect'$ip adb connect $ip }else{ write-host 'adb connect'$x adb connect $x } adb root } function wad{ write-host 'adb devices' adb devices } function was{ write-host 'adb shell' adb shell } function wa($param){ if ($param -eq 'server'){ write-host 'adb kill-server' adb kill-server write-host 'adb start-server' adb start-server wad }elseif ($param -eq 'remount'){ write-host 'adb remount' adb shell 'mount -o remount,rw /system' } } function wak($value){ if ($value -eq 'home'){ write-host 'key home 3' adb shell input keyevent "3" }elseif ($value -eq 'menu'){ write-host 'key menu 82' adb shell input keyevent "82" }elseif ($value -eq 'back'){ write-host 'key back 4' adb shell input keyevent "4" }elseif ($value -eq 'u'){ write-host 'key up 19' adb shell input keyevent "19" }elseif ($value -eq 'd'){ write-host 'key down 20' adb shell input keyevent "20" }elseif ($value -eq 'l'){ write-host 'key left 21' adb shell input keyevent "21" }elseif ($value -eq 'r'){ write-host 'key right 22' adb shell input keyevent "22" }elseif ($value -eq 'ok'){ write-host 'key ok 23' adb shell input keyevent "23" }else{ write-host 'key back null' } } function test { write-host 'function test' }
從以前的章節中,咱們知道PowerShell將一切存儲在對象中,那這些對象中包含了一系列中的稱之爲方法的指令。默認文本存儲在String對象中,它包含了許多很是有用的處理文本的命令。例如,要肯定一個文件的擴展名,可使用LastIndexOf()獲取最後一個字符「.」的位置,繼續使用Substring()獲取擴展名子串。
PS> $path = "C:\prefs.js" PS> $path.Substring( $path.LastIndexOf(".")+1 ) Js
另一條途徑,使用Split方法,對文件的完整名稱進行分割,獲得一個字符串數組,取最後一個元素,PowerShell中能夠經過索引-1來獲取數組中最後一個元素。
PS> $path.Split(".")[-1] Js
下面的表格會給出String對象的全部方法:
函數 | 描述 | 示例 |
CompareTo() | 與另外一個字符串比較 | (「Hello」).CompareTo(「Hello」) |
Contains() | 是否包含制定子串 | (「Hello」).Contains(「ll」) |
CopyTo() | 拷貝子串至新字符串中 | $a = (「HelloWorld」).toCharArray()(「User!」).CopyTo(0,
$a, 6, 5)$a |
EndsWith() | 是否以制定子串結尾 | (「Hello」).EndsWith(「lo」) |
Equals() | 是否與另外一個字符串相同 | (「Hello」).Equals($a) |
IndexOf() | 返回第一次匹配的所索引 | (「Hello」).IndexOf(「l」) |
IndexOfAny() | 返回字符串中任意字符的首次匹配索引 | (「Hello」).IndexOfAny(「loe」) |
Insert() | 在指定位置插入字符串 | (「HelloWorld」).Insert(6,」brave 「) |
GetEnumerator() | 枚舉字符串中全部字符 | (「Hello」).GetEnumerator() |
LastIndexOf() | 字符的最後匹配位置 | (「Hello」).LastIndexOf(「l」) |
LastIndexOfAny() | 任意字符的最後匹配位置 | (「Hello」).LastIndexOfAny(「loe」) |
PadLeft() | 左邊補齊空白是字符串至指定長度 | (「Hello」).PadLeft(10) |
PadRight() | 右邊填充空白是字符串至指定長度 | (「Hello」).PadRight(10) + 「World!」 |
Remove() | 從指定位置開始移除指定長度 | (「PsTips」).Remove(2,2) |
Replace() | 替換指定字符串 | (「PsTips」).replace(「Ps」,」PS1″) |
Split() | 以指定分隔符切割字符串 | (「HelloWorld」).Split(「l」) |
StartsWith() | 是否以指定子串開始 | (「HelloWorld」).StartsWith(「He」) |
Substring() | 從指定位置取指定長度子串 | 「HelloWorld」).Substring(4,3) |
ToCharArray() | 轉換成字符數組 | (「HelloWorld」).toCharArray() |
ToLower() | 轉換成小寫 | (「HelloWorld」).toLower() |
ToLowerInvariant
() |
以區域規則轉換成小寫 | (「HelloWorld」).ToUpperInvariant() |
ToUpper() | 轉換成大寫 | (「HelloWorld」).ToUpper() |
ToUpperInvariant
() |
以區域規則轉換成大寫 | (「HelloWorld」).ToUpperInvariant
() |
Trim() | 移除字符串先後空格 | (」 HelloWorld 「). Trim() |
TrimEnd() | 移除字符串結尾的空格 | (「HelloWorld 「). TrimEnd() |
TrimStart() | 移除字符串開始的空格 | (」 HelloWorld」). TrimStart() |
Chars() | 返回指定位置的字符 | (「Hello」).Chars(0) |
以Split()爲例來分析方法
在以前的章節中,咱們已經知道能夠經過Get-Member來查看一個對象中包含了那些能夠被調用的方法。正好最爲一個簡單的回顧,來查看Split的定義。
PS C:\> ("Pstips.net" | Get-Member Split).definition string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] separator, System.StringSplitOptions options), string[] Split(char[] separator, int count, System.StringSplitOptions options), string[] Split(string[] separator, System.StringSplitOptions options), string[] Split(string[] sepa rator, int count, System.StringSplitOptions options)
Define屬性能夠獲取方法參數定義,可是可讀性比較坑爹。咱們仍然用上面表格中的Replace方法,將分隔符稍做替換,便可加強可讀性。
PS C:\> ("Pstips.net" | Get-Member Split).definition.Replace("), ", ")`n") string[] Split(Params char[] separator) string[] Split(char[] separator, int count) string[] Split(char[] separator, System.StringSplitOptions options) string[] Split(char[] separator, int count, System.StringSplitOptions options) string[] Split(string[] separator, System.StringSplitOptions options) string[] Split(string[] separator, int count, System.StringSplitOptions options)
以前說過反引號,相似高級語言中的轉義符反斜槓。
從上面的輸出能夠發現Split有6種不一樣的調用方法,而以前可能更多的只使用過一個參數的方法。PowerShell在處理文本時,可能會碰到多個分隔符,而Split方法調用只須一次便可。
PS C:\> "http://www.pstips.net".split(":./") http www pstips net
中間有空白,咋整,能移除嗎,StringSplitOptions輕裝上陣:
PS C:\> "http://www.pstips.net".split(":./",[StringSplitOptions]::RemoveEmptyEntries) http www pstips net
以前有一個小算法題,移除字符串中相鄰的重複的空格。在不考慮效率的前提下,可使用Split先分割,分割後再將獲得的元素以指定分隔符拼接。可是拼接用到的Join方法,並不屬於string對象,而屬於String類,也正是下面要講的。
http://www.pstips.net/string-object-methods.html