2017-07-31 63 views
0

在PowerShell脚本中有一种内置的方式来重新创建一个目录,我倾向于在很多不同的脚本中反复使用它,而它做的工作,它导致混乱,我可以做一个函数,但我最终会复制粘贴它,只是想知道是否有一个更直接的方法来做到这一点。简单的内置方式来创建或删除并创建一个目录

If(Test-path $destination) 
{ 
    Remove-item $destination -Force -Recurse 
} 
New-Item $destination -type directory 
+1

我不知道如何创建自己的功能(然后你就可以把你的'$配置文件“或您在所有脚本中使用的模块)比执行相同任务(不存在)的内置功能更”杂乱“。 – alroc

+0

,因为这些脚本在许多不同的机器上使用,我不想在许多地方设置通用脚本的问题 –

+0

为什么不能像分发其余部分一样分发模块/通用脚本脚本? – alroc

回答

0

不,没有办法在Powershell的单个命令中删除和创建一个目录。除非诉诸于所提及的功能或模块方法,否则它将至少为2行。

你可以实现与同:

Remove-item $destination -Force -Recurse -ErrorAction SilentlyContinue 
New-Item $destination -type directory 

使其成为内联函数:

Function RecreateDirectory([string]$destination) { 
    Remove-item $destination -Force -Recurse -ErrorAction SilentlyContinue 
    New-Item $destination -type directory 
} 

RecreateDirectory("C:\OneDir") 
RecreateDirectory("C:\AnotherDir")