在PowerShell中使用Get-ChildItem时,如何获得询问路径的提示?

Mar*_*ter 3 powershell prompt path set

我是PowerShell的新手.我找到了一些指南并将一个小脚本混合在一起,但我似乎无法设置提示来询问源/目的地.

脚本如下:

gci -path   | Get-Random -Count 4 | mi -Destination C:\Temp
while(1) { sleep -sec 20; .\Power.ps1 }
Run Code Online (Sandbox Code Playgroud)

如有任何回复,请提前感谢!

ste*_*tej 5

用途Read-Host:

Get-ChildItem -Path (Read-Host -Prompt 'Get path')
Run Code Online (Sandbox Code Playgroud)


mjo*_*nor 5

这是一个例子,FWIW:

 $source,$target = $null,$null

while ($source -eq $null){
$source = read-host "Enter source file name"
if (-not(test-path $source)){
    Write-host "Invalid file path, re-enter."
    $source = $null
    }
elseif ((get-item $source).psiscontainer){
    Write-host "Source must be a file, re-enter."
    $source = $null
    }
}

while ($target -eq $null){
$target = read-host "Enter source directory name"
if (-not(test-path $target)){
    Write-host "Invalid directory path, re-enter."
    $target = $null
    }
elseif (-not (get-item $target).psiscontainer){
    Write-host "Target must be a directory, re-enter."
    $target = $null
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 无论如何要提供制表符补全吗? (2认同)