Powershell_Script<3>

发布时间:2019-07-05 10:30:37编辑:auto阅读(1605)

    Powershell 对文件的操作详解



    echo "Using the get-service cmdlet."

    #Histroy :

    # 2013/9/6

    get-service

    **获得服务信息命令

    echo "List the service that stopped "

    Get-Service | Where-Object {$_.status -eq "stopped"}

    //查看已经停止的服务对象

    echo "List the service that running "

    Get-Service | Where-Object {$_.status -eq "running"}

    //查看正在运行的服务对象。


    echo "List the service and sort them "

    Get-Service | Sort-Object status, display

    //查看系统运行的服务并按状态进行排序。


    cls

    echo  "Using powershell to operate files. "


    echo "Copy file ."

    **复制文件 cp C:\hello.txt C:\test

    copy-item C:\hello.txt C:\test

    ls  hello.txt

    **删除文件 rm hello.txt

    remove-item hello.txt

    **当前目录下创建以文件夹 new-item -type directory  test

    mkdir test

    echo "copy all subfolders to new folder."

    **复制多个文件 cp c:\*.txt C:\test

    copy-item C:\*.txt C:\test

    ls  *.txt

    ** 删除多个文件

    remove-item *.txt


    echo "Copy an floder to another ."

    * 文件夹的复制 cp C:\inetpub C:\test

    copy-item C:\inetpub C:\test -recurse

    ls inetpub

    ** 删除文件 rm inetpub

    remove-item  inetpub  -recurse


    echo "Creating an new directory."

    **创建以文件夹 mkdir C:\test\tst

    new-item C:\test\tst -type directory

    ls

    remove-item tst -recurse


    echo "Creating an new file "

    **创建文件

    new-item C:\test\hh.txt -type file

    remove-item hh.txt -recurse


    echo "Overwrite an file that exisits. "

    **覆盖一个文件

    new-item C:\test\test.bat -type file -force


    echo "Overwrite an file that exisits and write something in it."

    "oooooo" > test.bat

    echo "the content of test.bat"

    cat test.bat

    new-item C:\test\test.bat -type file -force -value "This was write into file ."

    echo "the content of test.bat"

    cat test.bat


    echo "Using the Remove-Item Cmdlet "

    **删除文件操作命令

    echo "remve file "

    new-item C:\test\tst.txt -type file

    ls

    remove-item tst.txt -recurse

    echo "after remove"

    ls


    echo "remove all things of an folder"

    批量创建、删除文件

    $ii=1,2,3,4,5,6,7,8

    foreach($i in $ii)

    {

    new-item  C:\test\test\file_$i  -type file

    }

    echo "Those file need to remove :"

    ls test

    remove-item C:\test\test\*

    echo "After remove files ."

    ls test

    echo "---"


    echo "Remove with  -exclude paramer "

    new-item C:\test\tt.wav -type file

    ls

    remove-item C:\test\* -exclude *.ps1

    echo "After remove :"

    ls


    echo "Remove with  -include paramer "

    $ii=1,2,3,4,5,6,7,8

    foreach($i in $ii)

    {

    new-item  C:\test\file_$i  -type file

    }

    ls

    remove-item C:\test\* -include file*

    echo "After remove :"

    ls


    echo "Remove with  -whatif paramer "

    $ii=1,2,3,4,5,6,7,8

    foreach($i in $ii)

    {

    new-item  C:\test\file_$i.vbs  -type file

    }

    ls

    remove-item C:\test\*.vbs -whatif

    rm *.vbs

    echo "After remove :"

    ls


    echo "Move a file or folder "

    **移动文件操作命令

    new-item C:\tt -type file

    echo "move  c:\tt to C:\test\"

    ls C:\

    ** mv C:\tt C:\test\

    move-item C:\tt C:\test\

    echo "After move :"

    ls

    rm C:\test\tt


    echo "move file with *"

    **批量移动文件

    $ii=1,2,3,4,5,6,7,8

    foreach($i in $ii)

    {

    new-item  C:\file_$i -type file

    }

    ls C:\

    move-item C:\file_*  C:\test\

    echo "After  move C:\"

    rm C:\file_*

    ls C:\

    echo "After move C:\test\"

    ls

    rm file_*


    echo "move with -force"

    ls C:\

    Move-Item c:\hello.txt c:\test -force

    echo "After move "

    ls

    Move-Item  c:\test\hello.txt  c:\ -force


    echo "rename file or folder"

    **重命名文件命令mv

    new-item C:\test\tt.txt -type file

    ls

    echo "After rename:" mv C:\test\tt.txt aa.txt

    rename-item C:\test\tt.txt aa.txt

    ls

    rm *.txt


    echo "get-childitem command :"

    get-childitem -recurse

    echo "Get-ChildItem env:

    "

    Get-ChildItem env:


    echo "get item with rules ."

    **查看注册表的信息

    Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

    **寻找文件--按条件

    echo "get item with -include"//包含条件

    get-childitem C:\* -include *.txt,*.log


    echo "get item with -exclude"//不包含条件

    Get-ChildItem c:\* -exclude *.txt,*.log


    echo "get item by sort "

    **查看文件按文件大小排序:

    Get-ChildItem c:\inetpub\* | Sort-Object length


    echo "sort desc:"

    **根据文件的长度排序文件

    Get-ChildItem c:\inetpub\* | Sort-Object length -descending


    echo "Get-Item :"

    **获取最近对文件的操作信息

    $(get-item c:\).lastaccesstime


    echo "subkeycount"

    **

    $(Get-Item hkcu:\software).subkeycount


    echo "member of ..."

    **获得注册表中应用程序的成员

    Get-Item hkcu:\software | Get-Member


    echo "Using the Test-Path Cmdlet"

    test-path C:\test\u_op*


    echo "use test-path hkcu Did someone ask if you can you check for the existence of registry keys using Test-Path? Of course you can:


    "

    **测试路径的存在

    Test-Path HKCU:\Software\Microsoft\Windows\CurrentVersion




关键字