PowerShell 2.0 實踐(一)操作檔案系統

來源:互聯網
上載者:User

PowerShell是微軟著力打造的下一代指令碼語言,基於.NET Framework,功能強大,易於擴充,與微軟及其第三方公司的許多產品進行了整合。Windows 7和Windows Server 2008 R2已經內建了PowerShell 2.0,其他動作系統需要單獨下載,可以在這裡下載。

樣本指令碼下載

本系列所有指令碼均在Windows Server 2008 R2(PowerShell 2.0) + PowerGUI Script Editor中測試通過。

 

PowerShell 開發工具:

1、Windows PowerShell ISE:微軟提供的一種輕量級的Power Shell IDE,預設存放在

%SystemRoot%\WINDOWS\system32\WindowsPowerShell\v1.0

 

支援代碼著色、斷點調試等,但是不支援智能感知:

2、PowerGUI Script Editor:Quest公司出品,是一個優秀的IDE,分為免費版與專業版,官方首頁。

方便的智能感知與文法提示,同樣支援斷點調試:

我將使用免費版的PowerGUI Script Editor進行測試。

3、PowerShell Plus:另一種強大的IDE,商業軟體,官方首頁。

 

推薦資源:

TechNet PowerShell首頁

TechNet PowerShell指令碼中心

國人的PowerShell V2部落格,很有深度,每月更新,推薦閱讀。

一個PowerShell社區

PowerGUI社區,包含了很多擴充、樣本、部落格等。

Idera PowerShell首頁,包含了很多擴充、樣本、部落格等。

微軟PowerShell官方部落格

 

下面我們來看一下PowerShell在檔案系統中的應用。

PowerShell中沒有檔案的概念,只有項【Item】的概念,像Windows磁碟機、目錄、檔案、註冊表、變數等資訊都被抽象成了"項"的概念,PowerShell使用"項"來進行統一管理。本次我們只關注檔案系統,即目錄、檔案,其他系統後續篇章再做瞭解。

關於檔案系統的命令有如下一些:

New-Item

Copy-Item

Move-Item

Remove-Item

Rename-Item

Invoke-Item

 

當然,這些命令不僅僅用於檔案系統,像磁碟機、註冊表等都可以使用,本次我們只從檔案系統的角度去關注這些命令。

 

  1. New-Item:在指定位置建立檔案或目錄。

1.1、建立一個目錄:

New-Item
-Path
E:\PSTest\NewDir
-ItemType
Directory
–Force

在E盤建立了PSTest\NewDir的目錄結構,ItemType指出建立的項的類型,這裡是目錄"Directory",Force表明若目錄結構存在則覆蓋。

1.2、建立一個檔案:

New-Item
-Path
E:\PSTest\NewDir\new.txt
-ItemType
File
–Force

在E:\PSTest\NewDir建立了new.txt文字檔,ItemType類型為File,存在則覆蓋。

還有很多參數,如WhatIf:預判如果執行該命令將發生什麼等,詳細參數列表及其用法請參閱PowerShell文檔,這裡只列出了最常用的。

 

2、Copy-Item:複製檔案、目錄。

2.1、複製檔案:

Copy-Item
-Path
E:\PSTest\NewDir\new.txt
-Destination
D:\
-PassThru
–Force

將一個檔案從E盤的一個目錄複寫到D盤,若存在則覆蓋。PassThru會強制PowerShell返回命令執行後的結果,方便查看命令是否執行成功。看以看出,Move-Item支援跨磁碟機複製檔案。

2.2、複製目錄:

Copy-Item
-Path
E:\PSTest\NewDir
-Destination
D:\
-PassThru
-Force
–Recurse

將E盤中的一個目錄複寫到D盤,若存在則覆蓋。請注意"Recurse"這個參數,只有加上這個參數PowerShell才會複製來源目錄中的所有子目錄及其檔案,否則只會複製來源目錄一個空殼。

 

3、Move-Item:移動檔案、目錄,相當於剪下。

3.1、移動檔案:

Move-Item
-Path
E:\PSTest\NewDir\new.txt
-Destination
E:\
-PassThru
–Force

移動一個檔案到根目錄,若存在則覆蓋,支援跨磁碟機移動:

Move-Item
-Path
E:\PSTest\NewDir\new.txt
-Destination
D:\
-PassThru
–Force

3.2、移動目錄:

Move-Item
-Path
E:\PSTest\NewDir
-Destination
E:\a\NewDir
-PassThru
–Force

移動一個目錄,若存在則覆蓋,注意來源目錄與目標目錄不能位於同一目錄中,且不能跨磁碟機移動目錄:

Move-Item
-Path
E:\PSTest\NewDir
-Destination
D:\
-PassThru
–Force

Error:

Move-Item : Source and destination path must have identical roots. Move will not work across volumes

.

At line:1 char:10

+ Move-Item <<<< -Path E:\PSTest\NewDir -Destination D:\ -PassThru -Force

+ CategoryInfo : WriteError: (E:\PSTest\NewDir:DirectoryInfo) [Move-Item], IOException

+ FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

 

若需要跨磁碟機移動目錄,則可以先複製,再刪除。

注意:

1、Move-Item可以跨磁碟機移動檔案,目錄不可以。

2、Copy-Item 檔案、目錄均可以跨磁碟機複製。


 4、Remove-Item:刪除檔案、目錄。

4.1、刪除檔案:

Remove-Item
-Path
E:\PSTest\NewDir\new.txt
-Recurse

4.2、刪除目錄:

Remove-Item
-Path
E:\PSTest\NewDir

若目錄中包含檔案,則會提示:

若不想提示就加上-Recurse參數,想提示加上-Confirm。

 

 5、Rename-Item:重新命名檔案、目錄。

5.1、重新命名檔案:

Rename-Item
-Path
E:\PSTest\NewDir\new.txt
old.txt
–PassThru

5.2、重新命名目錄:

Rename-Item
-Path
E:\PSTest\NewDir
OldDir
–PassThru

 

 6、Invoke-Item:開啟檔案、目錄。

6.1、開啟檔案:

Invoke-Item
C:\Windows\win.ini

Windows會調用指定檔案的預設程式來開啟檔案,就像雙擊開啟檔案一樣:


6.2、開啟目錄:

Invoke-Item
C:\Windows

與開啟檔案類似,調用Windows資源管理員。

 

小結:

本次我們學習了PowerShell檔案系統操作的基本命令,用了常見的參數,其他參數的用法可以參考文檔。PowerShell的命令相當靈活,很多參數可以自由組合,順序不定,給管理員提供了極大的便捷性。檔案系統是電腦管理的基礎部分,後續篇章會繼續關注其他部分。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.