Jump to content

PowerShell default response to read-host


Recommended Posts

Posted

Hi,

 

I'm using the following in a PowerShell script to answer a question

 

[font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500]$WantCompression[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#a9a9a9][font=Lucida Console][size=1][color=#a9a9a9][font=Lucida Console][size=1][color=#a9a9a9]=[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff]Read-Host[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000]"Do you want to compress the files/folders? Y/N"[/color][/size][/font][/color][/size][/font][/color][/size][/font]

 

I want to have a default response if enter is pressed if possible!?

Posted
Hi,

 

I'm using the following in a PowerShell script to answer a question

 

[font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500]$WantCompression[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][color=#a9a9a9][font=Lucida Console][size=1][color=#a9a9a9][font=Lucida Console][size=1][color=#a9a9a9]=[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff]Read-Host[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000]"Do you want to compress the files/folders? Y/N"[/color][/size][/font][/color][/size][/font][/color][/size][/font]

 

I want to have a default response if enter is pressed if possible!?

 

An IF statement afterwards would be the best way. Something like:

 

if ($WantCompression -notmatch "^[nyNY]+$") 
{
$WantCompression = "Y"
}

 

That would mean that if they don't enter a Y or N (upper of lower case) then it defaults to Y (or N if you'd prefer)

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...