Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

Read-Host Path With Space in PowerShell

July 11, 2024
in A, Blog, Powershell
0
ADVERTISEMENT

Table of Contents

Read-Host Path With Space in PowerShell

We have a simple Powershell script to get items in a folder. We use the Read-Host cmdlet that catch the folder path input from the user and store path as as strings in a variable. This makes life easier as we can drag and drop rather than full typing the path.

$path = Read-Host "Enter the path"
Write-Host "The path you've entered: $path" -ForegroundColor Green
Get-ChildItem -Path $path | Select-Object Name

Normally, when the path has space, user will round it with a single or double quotes.

VatCtmdm6VyfJ8qhBo2irRWSAyzAPGS0Jg4Vj3wRFwoEOqAOI3P5tqp6YFrc

As you can see, PowerShell though all string from Read-Host to the variable. So, the Get-ChildItem will return error becase the path is not valid.

If user input the path without any quotes. The script works normally.

4Oq7QCP9fB61EHB7OSLDeVKpamggTXsdt70WW2GAA29w67DVp2S4wKkNW6JA

Removing the quotes

To make it work all the time regardless user round the path with or without quotes. We need to remove the quotes from the $path variable before using it in the Get-ChildItem cmdlet.

$path = Read-Host "Enter the path"
$path = ($path).trim('"')
$path = ($path).trim("'")
Write-Host "The path you've entered: $path" -ForegroundColor Green
Get-ChildItem -Path $path | Select-Object Name

Now, regardless user input the path with a single quote or double quotes. The script will work normally.

3QNmckeIGJj5wADXmjXeLB6p5lrFIwgzCOTaMUjHyTq7qQIhyMqgTFKv5PLr
gBil7yKDKVX2iU4w17FRp8BIMFmgYlzAfhZCjyJNdwXrnA8x4zBqi93BuU8O
fIPkkT0u0ZRtYvNumEQ5Lc90sdbBQJxhUEtwcKo7tzcN2sXzw7qgEikXZJyF
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Trim Quotes From Read-Host Path Stored as a Variable

Next Post

Spaces Cause Split in Path With PowerShell

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory