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

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

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

Table of Contents

Trim Quotes From Read-Host Path Stored as a Variable

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.

The problem is that PowerShell keeps throwing errors, saying it can’t find the drive. This seems to be being caused by the quotes around the path. If we remove them after dragging and dropping works fine.

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

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.

c5aCYP51nmWgcHn2nPnnnpw2ehwBEdFEe86FAM9vOJ5vVVRHIhbvE5keqe5L

Removing the double quotes

To fix it, after store the strings from Read-Host to the variable. We need to remove the double quotes from the variable before using it in the Get-ChildItem.

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

As you can see, the script works as expected.

s2fPpUkyDJjtfTp68KWPdzfrIKMEdJlEDgAU57t9OsQ08dBKkreeZtnP66V8

Removing the single quote

In some cases, user rounds the path with a single quote instead of double quotes. The error comes again.

Y3PbGV9ICwEwFIMQ69dvtICoXd1l6b2iw8woZUivPMUhLsRIwcuPSKqYSCEP

To make it work, we need an addition step to remove the single quote from the variable as follows:

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

AXe8IAPnen6RBY040IQRIFI34kRiAW4qAy9tvuPdeI1QEo665xs4fyUiSbk9
eLMNtjNGAOXBlA1m0sVaJBHIz1RPycEotJTmLHl7boHfRvIKSJ11LDLncdVI
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Fix Winget Error 0x8a15000f : Data Required By The Source is Missing

Next Post

Read-Host Path With Space in 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