Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I want to pass below string parameter from cmd to ps1. The string that I want to pass has spaces in between. As a result, I believe, it is not able to read the entire string.

string parameter -
\\Intranet.abc.com\afhb-emea\GROUP\IND\IT\SharedServices\Pune Ravet\Test

Please help me how can I pass such string parameter containing spaces in between so that I can correctly read it in the powershell.

What I have tried:

I am passing string parameter to ps1 from cmd as below :

powershell.exe archive_files.ps1 -path "\\Intranet.abc.com\afhb-emea\GROUP\IND\IT\SharedServices\Pune Ravet\Test" -days 0 -months 0 -years 2

I am reading above string in powershell script as below :

param (
[string]$path,
[int]$days,
[int]$months,
[int]$years
)
write-host "Files to delete from Path is $path"

It is giving below output once I run it :

Files to delete from Path is \\Intranet.abc.com\afhb-emea\GROUP\IND\IT\SharedServices\Pune
Posted
Updated 24-Aug-22 6:12am
Comments
Jo_vb.net 24-Aug-22 8:35am    
Try the ideas which were posted @
https://stackoverflow.com/questions/28311191/how-do-i-pass-in-a-string-with-spaces-into-powershell

1 solution

Just put it in double quotes:
PowerShell
Foo.ps1 "\\Intranet.abc.com\afhb-emea\GROUP\IND\IT\SharedServices\Pune Ravet\Test"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900