设为首页 收藏本站
查看: 2419|回复: 0

Powershell_Script<3>

[复制链接]

尚未签到

发表于 2018-9-3 07:13:21 | 显示全部楼层 |阅读模式
  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
  ls  hello.txt
  **删除文件 rm hello.txt
  remove-item hello.txt
  **当前目录下创建以文件夹 new-item -type directory  test
  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-item  inetpub  -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-item  C:\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-item  C:\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-item  C:\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;move  c:\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-item  C:\file_$i -type file
  }
  ls C:\
  move-item C:\file_*  C:\test\
  echo &quot;After  move 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-Item  c:\test\hello.txt  c:\ -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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-561762-1-1.html 上篇帖子: Powershell_Script<2> 下篇帖子: How to learn powershell from zero-Security
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表