学习windows powershell(文件系统导航)

来源:互联网 发布:提问的软件 编辑:程序博客网 时间:2024/04/30 08:34

其实系统注册表和文件系统视作同样的东西进行处理的,由于注册表我一般不去操作,并且我也非常的不喜欢注册表,因此,注册表的操作我就不在这儿说明,仅列出文件系统(filesystem)的操作.

一、location (我在什么位置,将要操作哪个位置)

首先是get-help,有哪些有关于location操作的

PS C:/> get-help *-location |format-table -autosize -wrap

Name          Category Synopsis
----          -------- --------
Get-Location  Cmdlet   获取当前工作位置的相关信息。
Set-Location  Cmdlet   将当前工作位置设置为指定的位置。
Push-Location Cmdlet   将当前位置推入堆栈。
Pop-Location  Cmdlet   将当前位置更改为最近推入到堆栈中的位置。您可以从默认堆栈
                       中或从您使用 Push-Location 创建的堆栈中弹出位置。

get-location操作最无聊,因为你一眼就能看出当前的位置,如:

PS C:/> get-location

Path
----
C:/

 

PS不用说是指powershell ,位置,输入提示符。get-location就取中间的C:/(位置),

PS C:/> set-location //tom/d$
PS Microsoft.PowerShell.Core/FileSystem:://tom/d$> get-location

Path
----
Microsoft.PowerShell.Core/FileSystem:://tom/d$

这个信息则更完整,就连提供程序(Microsoft.PowerShell.Core/FileSystem)都显示出来了,还有几种提供程序的,可以从下面这个命令的结果中看出:

PS Microsoft.PowerShell.Core/FileSystem:://tom/d$> get-psdrive

Name       Provider      Root
----       --------      ----
Alias      Alias
C          FileSystem    C:/
cert       Certificate   /
D          FileSystem    D:/
E          FileSystem    E:/
Env        Environment
F          FileSystem    F:/
Function   Function
G          FileSystem    G:/
H          FileSystem    H:/
HKCU       Registry      HKEY_CURRENT_USER
HKLM       Registry      HKEY_LOCAL_MACHINE
Variable   Variable

除了fs还有registry/env/alias/variable/function等,我目前看到的也就上面的几种类型.

set-location类似于CD,把某个位置设置为当前工作位置,有一个参数-passthru,其实也就是将变化后的位置显示一下,个人感觉这个参数没有多少意义,实在不知道变化后的位置,那就get-location一下,如果命令非常复杂,那更用不着这个参数,因为你在操作非常复杂的命令的时候能不小心吗?能不知道将要去哪个位置吗?

push和pop是一对合作伙伴,时间长的关系好,后进先出,也就是栈的概念,事实也是如此,请看以下操作:

PS C:/> push-location -path c:/windows
PS C:/Windows> push-location -path system32
PS C:/Windows/System32> set-location -path d:/
PS D:/> push-location 'D:/Program Files'
PS D:/Program Files> set-location -path 'D:/Program Files (x86)'
PS D:/Program Files (x86)> pop-location -passthru

Path
----
D:/


PS D:/> pop-location -passthru

Path
----
C:/Windows


PS C:/Windows> pop-location -passthru

Path
----
C:/

从上面不难看出,push-location 是将当前的位置存入到stack中,而不是将-path参数后指定的位置存入到stack中,然后再将当前工作位置设置为-path 中指定的位置,-path参数名可以省略。

二、item(操作的对象是什么)

item可以是文件,可以是文件夹,不同的提供程序下面,可以有不同的命名,但总的说来就是item.对于FS而言那就是文件夹和文件,老习惯,先来个所有的操作(我已经不喜欢get-command了):

PS C:/> get-help *-item |format-table -wrap -autosize

Name        Category Synopsis
----        -------- --------
Get-Item    Cmdlet   获取位于指定位置的项。
New-Item    Cmdlet   在命名空间中创建新项。
Set-Item    Cmdlet   将项的值更改为命令中指定的值。
Remove-Item Cmdlet   删除指定项。
Move-Item   Cmdlet   将项从一个位置移动到另一个位置。
Rename-Item Cmdlet   重命名 Windows PowerShell 提供程序命名空间中的一个项。
Copy-Item   Cmdlet   将项从一个位置复制到命名空间内的另一个位置。
Clear-Item  Cmdlet   删除项的内容,但不删除该项。
Invoke-Item Cmdlet   对指定项调用特定于提供程序的默认操作。

有一个给忘了,而且是非常高的频率使用的,忘了这个可就惨了:

1-可以使用通配符,通配符包括*/?/[],*-任意个数的任意字会,?-一个任意字符 []-括号中间的任何一个字符

2- -recurse参数,查找子目录

3- -exclude参数,不包含指定条件的项

4- -include参数,包含指定条件的项

5- -force显示隐藏的项

有这么多参数,我还是感觉很失望,.net对于正则表达式有了很好的支持,在查找文件的时候怎么不用上正则表达式呢?正想骂骂MS的猪们,把东西搞得很复杂,但是简单的强大功能却又不支持,真的是好笑。

搞个最复杂的来试验一下:

PS C:/> get-childitem -path c:/windows -include *.dll -recurse -exclude [a-y]*.d
ll
Get-ChildItem : 对路径“C:/windows/System32/LogFiles/WMI/RtBackup”的访问被拒绝

所在位置 行:1 字符: 14
+ get-childitem  <<<< -path c:/windows -include *.dll -recurse -exclude [a-y]*.
dll


    目录: Microsoft.PowerShell.Core/FileSystem::C:/windows/System32/spool/drive
    rs/w32x86/3


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2005/3/18     19:18      40960 ZGDI32.DLL
-a---         2005/3/18     19:18      45105 ZJBIG.dll
-a---         2005/3/18     19:18      86016 ZLhp1020.dll
-a---         2005/3/18     19:18      28672 zlm.dll
-a---         2005/3/18     19:18      86016 ZSPOOL.DLL
-a---         2005/3/18     19:18      24576 ZTAG32.DLL


    目录: Microsoft.PowerShell.Core/FileSystem::C:/windows/System32


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2008/1/19     15:37     342016 zipfldr.dll

 

用的win2008的环境,所以会有个拒绝访问的错误,我把UAC开着的,没有去关掉。

接下来来试验item了:

PS D:/temp> new-item -itemtype directory test1


    目录: Microsoft.PowerShell.Core/FileSystem::D:/temp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         2008/8/31     14:51            test1


PS D:/temp> set-location test1
PS D:/temp/test1> new-item -itemtype file -path file1.txt


    目录: Microsoft.PowerShell.Core/FileSystem::D:/temp/test1


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2008/8/31     14:52          0 file1.txt


PS D:/temp/test1> copy-item -path d:/out.txt -force -destination file1.txt
PS D:/temp/test1> ls


    目录: Microsoft.PowerShell.Core/FileSystem::D:/temp/test1


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2008/8/30     14:57      17198 file1.txt


PS D:/temp/test1> set-location -path ..
PS D:/temp> copy-item -path test1 -destination test2
PS D:/temp> set-location -path test2
PS D:/temp/test2> get-childitem
PS D:/temp/test2> set-location -path ..
PS D:/temp> copy-item -path test1 -destination test2 -recurse -force
PS D:/temp> set-location test2
PS D:/temp/test2> get-childitem


    目录: Microsoft.PowerShell.Core/FileSystem::D:/temp/test2


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         2008/8/31     14:54            test1


PS D:/temp/test2> set-location test1
PS D:/temp/test2/test1> ls


    目录: Microsoft.PowerShell.Core/FileSystem::D:/temp/test2/test1


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2008/8/30     14:57      17198 file1.txt

PS D:/temp> remove-item -path test1

确认
D:/temp/test1 处的项具有子项,并且未指定 Recurse
参数。如果继续,所有子项均将随该项删除。是否确实要继续?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 挂起(S)  [?] 帮助
(默认值为“Y”):y
PS D:/temp> remove-item -path test2

确认
D:/temp/test2 处的项具有子项,并且未指定 Recurse
参数。如果继续,所有子项均将随该项删除。是否确实要继续?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 挂起(S)  [?] 帮助
(默认值为“Y”):y

PS D:/> invoke-item out.txt
Invoke-Item : 找不到应用程序
所在位置 行:1 字符: 12
+ invoke-item  <<<< out.txt

(我没有为txt文件设置默认打开程序,该死的2008)
PS D:/>

 

 

需要说明的是:

1、new-item 使用-itemtype参数指定创建的是文件夹还是文件,操作注册表的键的时候,由于位置的缘故吧,不需要说明

2、move-item是剪切并粘贴,copy-item是复制并粘贴,如果复制一个文件夹,除非指定-recurse,否则不复制任何文件夹下的内容。

3、移除文件夹内容,将会有提示

4、-force参数,强制执行

5、rename-item 只能够改名,不能移动item

6、invoke-item 执行指定的项,相当于是double-click的作用了,如果没有任何程序(或者没有指定默认的程序)和一个非可执行程序item有默认打开关系,则会报错,否则使用默认打开的程序进行打开操作。

 

三、psdrive(驱动器是什么?)

上面已经使用get-psdrive命令来执行了一下,按照常规理解,也许就只是硬盘分区了,但事实上,提供程序不同,会有不同的drive,同时你也可以使用new-psdrive来新建PS驱动器,其实是先有PSDRIVE才有ITEM的,但是我几乎把这个PSDRIVE忽略了,事实上你也可以这么做,MS搞出这个东西,无非是就想和UNIX设备文件搞得有点相同的概念。

PS D:/> get-help *-psdrive|format-table -autosize -wrap

Name           Category Synopsis
----           -------- --------
New-PSDrive    Cmdlet   安装新 WIndows PowerShell 驱动器。
Remove-PSDrive Cmdlet   从所在位置删除 Windows PowerShell 驱动器。
Get-PSDrive    Cmdlet   获取有关 Windows PowerShell 驱动器的信息

 

操作如下:

PS D:/> new-psdrive -psprovider FileSystem -name officehome -root 'D:/Program Fi
les/Microsoft Office/OFFICE11'

Name       Provider      Root                                   CurrentLocation
----       --------      ----                                   ---------------
officehome FileSystem    D:/Program Files/Microsoft Offic...

PS D:/> set-location officehome:
PS officehome:/> ls


    目录: Microsoft.PowerShell.Core/FileSystem::D:/Program Files/Microsoft Offi
    ce/OFFICE11


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          2008/8/4      8:47            1033
d----          2008/8/4      8:47            2052
d----          2008/8/4      8:47            Migration
-a---         2003/7/15      6:57      38968 AUTHZAX.DLL
-a---         2003/7/15      6:53      94768 AW.DLL
-a---         2002/7/29     15:32      73728 BIDI32.DLL
-a---         2003/7/15      6:56      14904 DSITF.DLL
-a---         2003/7/15      6:57      98360 DSSM.EXE

......

PS officehome:/> remove-psdrive -name officehome
Remove-PSDrive : 无法删除驱动器“officehome”,因为它在使用中。
所在位置 行:1 字符: 15
+ remove-psdrive  <<<< -name officehome
PS officehome:/> set-location d:
PS D:/> remove-psdrive -name officehome

 

说到这里,文件和文件夹的操作就完工了,想想是有点简单了,或者是粗糙了,但是我相信如果熟悉cmd.exe的,一定能够用PS cmdlets来操作了.