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

January 13, 2026 4 answers
Asked by SkyLaKe92
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!

Answers

CodeNinja89
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.

Reply to this answer
DevDude42
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.

Reply to this answer
ScriptGuruX
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.
Reply to this answer
DocDev23
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!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions