285572001 发表于 2018-9-3 07:13:21

Powershell_Script<3>

  Powershell 对文件的操作详解
  echo &quot;Using the get-service cmdlet.&quot;
  #Histroy :
  # 2013/9/6
  get-service
  **获得服务信息命令
  echo &quot;List the service that stopped &quot;
  Get-Service | Where-Object {$_.status -eq &quot;stopped&quot;}
  //查看已经停止的服务对象
  echo &quot;List the service that running &quot;
  Get-Service | Where-Object {$_.status -eq &quot;running&quot;}
  //查看正在运行的服务对象。
  echo &quot;List the service and sort them &quot;
  Get-Service | Sort-Object status, display
  //查看系统运行的服务并按状态进行排序。
  cls
  echo&quot;Using powershell to operate files. &quot;
  echo &quot;Copy file .&quot;
  **复制文件 cp C:\hello.txt C:\test
  copy-item C:\hello.txt C:\test
  lshello.txt
  **删除文件 rm hello.txt
  remove-item hello.txt
  **当前目录下创建以文件夹 new-item -type directorytest
  mkdir test
  echo &quot;copy all subfolders to new folder.&quot;
  **复制多个文件 cp c:\*.txt C:\test
  copy-item C:\*.txt C:\test
  ls*.txt
  ** 删除多个文件
  remove-item *.txt
  echo &quot;Copy an floder to another .&quot;
  * 文件夹的复制 cp C:\inetpub C:\test
  copy-item C:\inetpub C:\test -recurse
  ls inetpub
  ** 删除文件 rm inetpub
  remove-iteminetpub-recurse
  echo &quot;Creating an new directory.&quot;
  **创建以文件夹 mkdir C:\test\tst
  new-item C:\test\tst -type directory
  ls
  remove-item tst -recurse
  echo &quot;Creating an new file &quot;
  **创建文件
  new-item C:\test\hh.txt -type file
  remove-item hh.txt -recurse
  echo &quot;Overwrite an file that exisits. &quot;
  **覆盖一个文件
  new-item C:\test\test.bat -type file -force
  echo &quot;Overwrite an file that exisits and write something in it.&quot;
  &quot;oooooo&quot; > test.bat
  echo &quot;the content of test.bat&quot;
  cat test.bat
  new-item C:\test\test.bat -type file -force -value &quot;This was write into file .&quot;
  echo &quot;the content of test.bat&quot;
  cat test.bat
  echo &quot;Using the Remove-Item Cmdlet &quot;
  **删除文件操作命令
  echo &quot;remve file &quot;
  new-item C:\test\tst.txt -type file
  ls
  remove-item tst.txt -recurse
  echo &quot;after remove&quot;
  ls
  echo &quot;remove all things of an folder&quot;
  批量创建、删除文件
  $ii=1,2,3,4,5,6,7,8
  foreach($i in $ii)
  {
  new-itemC:\test\test\file_$i-type file
  }
  echo &quot;Those file need to remove :&quot;
  ls test
  remove-item C:\test\test\*
  echo &quot;After remove files .&quot;
  ls test
  echo &quot;---&quot;
  echo &quot;Remove with-exclude paramer &quot;
  new-item C:\test\tt.wav -type file
  ls
  remove-item C:\test\* -exclude *.ps1
  echo &quot;After remove :&quot;
  ls
  echo &quot;Remove with-include paramer &quot;
  $ii=1,2,3,4,5,6,7,8
  foreach($i in $ii)
  {
  new-itemC:\test\file_$i-type file
  }
  ls
  remove-item C:\test\* -include file*
  echo &quot;After remove :&quot;
  ls
  echo &quot;Remove with-whatif paramer &quot;
  $ii=1,2,3,4,5,6,7,8
  foreach($i in $ii)
  {
  new-itemC:\test\file_$i.vbs-type file
  }
  ls
  remove-item C:\test\*.vbs -whatif
  rm *.vbs
  echo &quot;After remove :&quot;
  ls
  echo &quot;Move a file or folder &quot;
  **移动文件操作命令
  new-item C:\tt -type file
  echo &quot;movec:\tt to C:\test\&quot;
  ls C:\
  ** mv C:\tt C:\test\
  move-item C:\tt C:\test\
  echo &quot;After move :&quot;
  ls
  rm C:\test\tt
  echo &quot;move file with *&quot;
  **批量移动文件
  $ii=1,2,3,4,5,6,7,8
  foreach($i in $ii)
  {
  new-itemC:\file_$i -type file
  }
  ls C:\
  move-item C:\file_*C:\test\
  echo &quot;Aftermove C:\&quot;
  rm C:\file_*
  ls C:\
  echo &quot;After move C:\test\&quot;
  ls
  rm file_*
  echo &quot;move with -force&quot;
  ls C:\
  Move-Item c:\hello.txt c:\test -force
  echo &quot;After move &quot;
  ls
  Move-Itemc:\test\hello.txtc:\ -force
  echo &quot;rename file or folder&quot;
  **重命名文件命令mv
  new-item C:\test\tt.txt -type file
  ls
  echo &quot;After rename:&quot; mv C:\test\tt.txt aa.txt
  rename-item C:\test\tt.txt aa.txt
  ls
  rm *.txt
  echo &quot;get-childitem command :&quot;
  get-childitem -recurse
  echo &quot;Get-ChildItem env:
  &quot;
  Get-ChildItem env:
  echo &quot;get item with rules .&quot;
  **查看注册表的信息
  Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
  **寻找文件--按条件
  echo &quot;get item with -include&quot;//包含条件
  get-childitem C:\* -include *.txt,*.log
  echo &quot;get item with -exclude&quot;//不包含条件
  Get-ChildItem c:\* -exclude *.txt,*.log
  echo &quot;get item by sort &quot;
  **查看文件按文件大小排序:
  Get-ChildItem c:\inetpub\* | Sort-Object length
  echo &quot;sort desc:&quot;
  **根据文件的长度排序文件
  Get-ChildItem c:\inetpub\* | Sort-Object length -descending
  echo &quot;Get-Item :&quot;
  **获取最近对文件的操作信息
  $(get-item c:\).lastaccesstime
  echo &quot;subkeycount&quot;
  **
  $(Get-Item hkcu:\software).subkeycount
  echo &quot;member of ...&quot;
  **获得注册表中应用程序的成员
  Get-Item hkcu:\software | Get-Member
  echo &quot;Using the Test-Path Cmdlet&quot;
  test-path C:\test\u_op*
  echo &quot;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:
  &quot;
  **测试路径的存在
  Test-Path HKCU:\Software\Microsoft\Windows\CurrentVersion

页: [1]
查看完整版本: Powershell_Script<3>