Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 236
Mark Jenster Avatar asked Oct 19 '25 16:10

Mark Jenster


2 Answers

Use Read-Host:

Get-ChildItem -Path (Read-Host -Prompt 'Get path')
like image 195
stej Avatar answered Oct 22 '25 07:10

stej


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
    }
}
like image 34
mjolinor Avatar answered Oct 22 '25 08:10

mjolinor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!