why does command line execution gives different result to script execution in powershell

invoke-expression vs invoke-command
powershell invoke-command
powershell commands
powershell run command
powershell script
powershell script to run a command
invoke-expression vs start-process

Running the following command in powershell command prompt works as i would expect:

Get-Service "my_service"
 Status   Name               DisplayName                           
------   ----               -----------                           
Running  my_service          My_SERVICE   

However if make a ps1 scritp as follows my output is not what i would expect.

$my_service_check = Get-Service "my_service"
echo "My Service Status: $my_service_check"

my_service:  System.ServiceProcess.ServiceController 

why does the script not return the same output as the command?

It's more to do with where you have the quotation marks in the echo statement.

In your example, $my_service_check is a System.ServiceProcess.ServiceController object- it's not a string that just contains the data you see. You could call methods on it, for example- $my_service_check.Start().

Now if you had

echo "My Service Status:" $my_service_check

PowerShell is smart enough to realize you want to output the human-readable data in that object- and you get the pretty results.

Because you have

echo "My Service Status: $my_service_check"

PowerShell is displaying the object itself as a string- which is almost never what you actually want.

Scripting : Powershell script provides different results from SMA vs , I have a simple Powershell script that searches an "uninstall location" in the script provides different results from SMA vs directly at Command Prompt But I can open a Command Prompt window on the target computer Yep, these two commands, when executed via a KACE script, give identical results: It’s nice for simple script execution, but more complex scripts usually end up with parsing errors. This technique does not result in a configuration change or require writing to disk. Example 1: Full command. Powershell -command "Write-Host 'My voice is my passport, verify me.'" Example 2: Short command

Here is simple one:

echo "My Service Status:" $my_service_check

Different results between "PowerShell ISE" and "Run with , "{0} {1}" -f $_.name,$_.lastwritetime.toshortdatestring()} } cmd /c pause An example screenshot with the two different results for the first script: So, when you run your code in ISE that uses dd/mm/yyyy format, your In PowerShell console date format is mm/dd/yyyy , so your code This is my new line: To execute a batch file, type its name at a command prompt, or execute a Windows shortcut that does the same thing. To uninstall Groovy, run the following command from the command line or from PowerShell: Copy groovy to Clipboard NOTE: This applies to both open source and commercial editions of Chocolatey. Nifi execute groovy script example.

Using the below script seems to output the results as you want it:

$myService = Get-Service #SERVICE_NAME

Write-Host "My service: " $myService



My service: SERVICE_NAME

Press Enter to cotinue...:

Invoke-Expression, Without Invoke-Expression, a string submitted at the command line is to run a command that the user enters, verify that the command is safe to run before running it. In general, it is best to design your script with predefined input options, rather expressions using PowerShell's call operator and achieve the same results. I've got a folder that has 0600 chmod permission and this folder has a sh script. I'm trying to limit some permissions but I've realized that if the folder has not a execute permission, unless for

Top Ten: PowerShell Annoyances, I'm aware that there are reasons for all of these different features. up their hands and give up, which would be unfortunate because PowerShell is on Not being able to run PowerShell scripts from the command line—No wonder represent the current value and can also be used to filter pipeline results. To enable, run the enable_robot script from the baxter_tools package. the batch file is read, the command is executed, and then the batch file is reread in order to execute the next command, rinse and repeat until the end of the file, REM statements do in essence slow down the execution of a batch file – though taking.

Django RESTful Web Services: The easiest way to build Python , If you prefer Command Prompt, run the following command in the Windows command shows the virtual environment activated in Windows 10 Command Prompt, after executing the previously shown commands: As we can see from the have scripts execution enabled in Windows PowerShell to be able to run the script:  Measure-Command is a great Powershell cmdlet to quickly find how much a piece of code takes to execute. Let me give you a classic example of getting the same thing done with different methods; the comparison of using the Foreach-Object cmdlet with the pipeline, and the foreach statement. Each do the exact same thing, for the most part, yet they

Provide A Batch File To Run Your PowerShell Script From; Your , When I am writing a script for other people to use (in my organization, or for the We can't call the PowerShell script as admin from the command prompt, but we can I give the code for how to run the script as admin just a few lines below the not using it would result in an error being thrown instead of the script being ran. The bottom line is that PowerShell lacks a built-in way to see an executable's exact command line. To get around this limitation, I wrote a short command-line program named ShowArgs.exe. This program's purpose is to output its command-line parameters without any parsing or interpretation.

  • What output do you expect? Or are you just looking for an explanation why it happens?
  • In my script i was expecting to see output like i see when i run the command in the prompt
  • Thanks @Mark, that makes sense. Previously i have created a variable using get-content and select-string. Is that object a string because im explicitly saying get the content of the file and choose the string that matches where as here im not explicitly selecting a string?
  • Sort of. Select-String will actually return a Microsoft.PowerShell.Commands.MatchInfo object, it just happens that particular type of object works better when you display it. I suspect this is because that class was designed by the PowerShell team, while ServiceController comes from .NET Framework, and was designed with a language like C# or VB.NET in mind.
  • previously i have echoed text and my variable inside quotes. can you tell me why it works outside of quotes as you suggest but not inside?
  • echo "my service: $myservice" - it say's object.Tostring() , and here echo "my service:" $myservice we print object