简体   繁体   中英

How can i get a prompt to ask for the path, when using Get-ChildItem in PowerShell?

I'm rather new to PowerShell. I found a few guides and mashed a little script together, though i do not seem to be able to set a prompt to ask for the source/destination.

The script is as following:

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

For any response, thanks in advance!

使用Read-Host

Get-ChildItem -Path (Read-Host -Prompt 'Get path')

Here's an example, 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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM