Register
: :
Login
Home
News
Forums
Scripts
User Groups
Resources
About
Forums
Search
Members
Unanswered
Active Topics
Forums
>
Using PowerShell
>
General PowerShell
Command line arguments
Last Post 18 Mar 2010 06:07 AM by
Petri
. 2 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
Petri
New Member
Posts:5
14 Mar 2010 10:56 PM
Hi there,
Why my .ps1 script like below:
Write-host Test space and arguments: $args[0].
Gives me result:
Test space and arguments: ball .
When I call it as:
.\test.ps1 ball
Why there is additional space after "ball" ??
cameronove
Basic Member
Posts:332
15 Mar 2010 01:53 PM
I don't use write-host anymore (I had a hard time predicting out put from time to time). I'm using PowerShell v2 RTM. Not sure how this works on v1. But when I want to write to the console I just put what I want in between double-quotes. So all Test.ps1 needs to have is the following (including the double-quotes):
"Test space and arguments: $($args[0])."
.\test.ps1 ball
Should produce what you want at the console. If you want the output printed to a file you could do:
"Test space and arguments: $($args[0])." > filename.txt
The greater-than sign redirects the output to the file.
Now a bit of explanation:
You can use either single-quotes or double-quotes when specifying a string. However, double-quotes resolve variables.
For example:
$name = "Spot"
$Sentence = "See $name run."
This produces
See Spot run.
If, instead, you used single-quotes for the $Sentence variable:
$Sentence = 'See $name run.'
The output would be
See $name run.
Sometimes you want to access a noteproperty or even a statement that returns a string in a string:
$Pet.breed = 'Terrier'
$Pet.name = 'Spot'
you could access these noteproperties but not "straight up"
If you typed
"$Pet.name is a $Pet.breed"
it would return
@{breed=Terrier; name=Spot}.name is a @{breed=Terrier; name=Spot}.breed
To actually get the value you need to put the variable inside parentheses like so...
"$($Pet.name) is a $($Pet.breed)"
Anything inside $() will be resolved for example:
"$($Pet.name) ran a race on $(get-date -f MM/dd/yyyy)."
Would produce:
Spot ran a race on 03/15/2010.
Also anything in quotations on it' own line will be output to the console. So you don't have to write-host to output to the console.
Petri
New Member
Posts:5
18 Mar 2010 06:07 AM
Many thanks to you Cameron. The difference between double quota and singles were the answer. So this was enough:
"Test space and arguments: $args[0]."
--
Petri
You are not authorized to post a reply.
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Forums
>
Using PowerShell
>
General PowerShell
Active Forums 4.3
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2