How to Add a Yes/No Prompt in PowerShell Function?

0
10
Asked By SkyLaKe92 On

I'm working on a PowerShell function and want to incorporate a Yes/No prompt when it comes to creating a subfolder for templates. Basically, if the user selects 'Yes', the folder should be created, and templates will be copied there, after which the script should terminate. If the user selects 'No', it should skip copying templates entirely. Here's the code I have so far:

```powershell
function Test1() {
[cmdletBinding()]
param(
[Parameter(Mandatory)]
[String] $Name
)

$DesktopPath = [Environment]::GetFolderPath("Desktop")
$Foldername = (Get-Date -Format 'yyyyMMdd-HHmmss')
$ServerShare = "\serverusers"
mkdir (Join-Path -Path $ServerShare -ChildPath $Name)

########Create sub folder for templates
# Add prompt for creating folder here
mkdir (Join-Path -Path $DesktopPath -ChildPath $Foldername)
Copy-Item -Path "\servertemplates26" -Destination (Join-Path -Path $DesktopPath -ChildPath $Foldername) -Recurse
########Copy templates
Copy-Item -Path "\servertemplates26" -Destination $DesktopPath -Recurse
}
```
Any help would really be appreciated!

4 Answers

Answered By DocDev23 On

Don't forget to check the official documentation for `Read-Host` if you need more details about its usage. Here's a link to the docs: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/read-host?view=powershell-7.5. That could help clarify any other questions you might have!

SkyLaKe92 -

I did check the docs, but I couldn't connect the dots on how to integrate it. That's why I asked for help!

Answered By ScriptGuruX On

Here's a simple example of using a loop with prompts. This ensures the script keeps asking until the user inputs a valid response:

```powershell
$choice = ''
do {
$choice = Read-Host "Do you want to create the subfolder? (y/n)"
} until ($choice -match '^(y|n)$')
if ($choice -eq 'y') {
mkdir (Join-Path -Path $DesktopPath -ChildPath $Foldername)
Copy-Item -Path "\servertemplates26" -Destination (Join-Path -Path $DesktopPath -ChildPath $Foldername) -Recurse
}
```
This will keep prompting until a valid 'y' or 'n' is given.

Answered By DevDude42 On

If your script needs to confirm actions, consider using `SupportsShouldProcess` like this:

```powershell
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
```
Then you can call `ShouldProcess` to check if the action should proceed based on user input. This handles prompts for you by default and can also give a `-WhatIf` option, which is pretty handy for scripting.

SkyLaKe92 -

Thanks for this! I'll definitely look into `SupportsShouldProcess` for better user interaction.

Answered By CodeNinja89 On

You can achieve this by using the `Read-Host` cmdlet to prompt for input. Here's a modified version of your script where the prompt is integrated:

```powershell
$prompt1 = Read-Host "Create subfolder? (yes/no)"
if ($prompt1 -eq 'yes') {
mkdir (Join-Path -Path $DesktopPath -ChildPath $Foldername)
Copy-Item -Path "\servertemplates26" -Destination (Join-Path -Path $DesktopPath -ChildPath $Foldername) -Recurse
} else {
Write-Host "Skipping template copy."
}
```
This way, if the user selects 'No', it will simply skip the template copying part without breaking the script.

HelpfulHarry77 -

Looks good! Just ensure that the input check for `yes` or `no` is case-insensitive if that's a concern for user input.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.