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 Run a Script With The Path Has Spaces in PowerShell

June 27, 2024
in Blog, Powershell
0
ADVERTISEMENT

Table of Contents

In some cases, you want to run a PowerShell script from the command line from anywhere without navigate to the path of the script. By default, when start a new PowerShell session, it starts in the current user folder.

hZMPQPxAhV3ck1hBhdFBoJGESbdIz1iV6G8ELavbSEOeHbtR2fEz1hYFYWk4

But the script is saved in D:\my scripts folder. When typing the path of the script, it won’t work because the path has a space in it.

aeThimbYkrDw4y25DmRtdKgPo9IHhp98g1hjCbwz9VKWe9CBvHtKOqMTRTNx

If you cover the path with quotes, PowerShell thinks that it is a string and print it to screen instead of executing it.

rwHPZgNaizBAY9o1Im8Hr4z7sSjnCK1sgjEpvcoshlKsO1VbT1tz0jOAM76D

If the path of the script does not contain any space, the script can be run normally.

gQOuremmMOUbUCLM4OInYykeU05q0M1EApktbL72D6xikvzptRwLXrWzf1kn

To run the script without navigate to the script’s directory. You can try some methods below:

Dot-Sourcing in PowerShell

Dot-sourcing is a concept in PowerShell that allows you to reference code defined in one script in a separate one. This is useful when you’re working on a project with multiple scripts and might have functions specified in one script(s) and the code to call those functions in another.

. "D:\my scripts\test.ps1"
QnzZ3V3UdLgrdQKbsHnTNHYyKJ5yg5O9htw9BAJeTlhrW1TXcLGNAc0kU3WP

You can run another script from a script as well.

QqqcaEY9pgpQv8ZSHte4feyNKmA9uMpjo3D3AZlRrUUmIck08eTZw1XgrdaH

The call operator &

Alternatively, the PowerShell call operator (&) lets you run commands that are stored in variables and represented by strings or script blocks. You can use this to run any native command or PowerShell command. This is useful in a script when you need to dynamically construct the command-line parameters for a native command.

& "D:\my scripts\test.ps1"
GTGtTuACRWkJV3VTPmIei56I1YMrNHyfXfFHs4u5g3Y1LI7kDRuRY2ogZ8qx

The -File parameter

The third way, if you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes (“). Single quotes (‘) are only recognized by PowerShell. But as powershell.exe is invoked (and hence the file parameter processed) by the command line, you have to use “.

powershell.exe -ExecutionPolicy Bypass -File "D:\my scripts\test.ps1"
x3THdoZb0PytBjRJtzWTsV4cmPgv2FWSfYGNSXuSYXacfzBn5WgbhZ1dVy3T

The -Command parameter

The fourth way, if you use the -Command parameter, instead of -File, the -Command content is processed by PowerShell.

powershell.exe -ExecutionPolicy Bypass -Command "& 'D:\my scripts\test.ps1'"

The double quotes are processed by the command line, and & ‘D:\my scripts\test.ps1 is a command that is actually processed by PowerShell.

Fpxjlk7rfddYRG5HjczNcX746foSbGiVN8Tj0ZNW8eYN4vTrMZBuhX8j79h8

The -Command is also the default parameter that is used, if you do not specify any.

powershell.exe "& 'D:\my scripts\test.ps1'"
oQaOPuczI6hr05sCWm4EoTECOaFC9rinJHMpRHJCQiEnxcDHyEFD0JonasWx

The -EncodedCommand parameter

And the last way, you can encode your command as Base64. This solves many “quoting” issues and is sometimes (but not in your case though) the only possible way.

First you have to create the encoded command:

$command = "& 'D:\my scripts\test.ps1'"
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($command))

And then you can use the the -EncodedCommand parameter like this:

powershell.exe -EncodedCommand JgAgACcARAA6AFwAbQB5ACAAcwBjAHIAaQBwAHQAcwBcAHQAZQBzAHQALgBwAHMAMQAnAA==
Kya88YDsssII8RtZK690OpVKPZUvaMCsD1BeWMC7jzEJWSKB0XoGBy3UB9Mn
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Fix Cannot Install New Microsoft Teams Because It Uses Certain Restricted Capabilities

Next Post

Download Links for Windows Management Framework 4.0 Download (WMF 4.0)

Related Posts

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
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 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