# How to execute a powershell script?

**URL:** <https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997>\
**Category:** Chef Infra (archive)\
**Created:** [July 22, 2016, 7:48pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997 "2016-07-22T19:48:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![erbeh77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/erbeh77/32/1508_2.png) [@erbeh77](https://discourse.chef.io/u/erbeh77)\
**Post date:** [July 22, 2016, 7:48pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997/1 "2016-07-22T19:48:34Z")

</div>

I am looking to execute a powershell script within a cookbook…

execute ‘test hello’ do  
command 'c:\temp\hello.ps1’  
action :run  
end

It just opening up the file instead, how do I execute it?

---

<div class="post-metadata">

**Author:** ![Matt\_Wrock](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/matt_wrock/32/3394_2.png) [@Matt\_Wrock](https://discourse.chef.io/u/Matt_Wrock)\
**Post date:** [July 22, 2016, 8:42pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997/2 "2016-07-22T20:42:22Z")

</div>

You want to use the `powershell_script` resource. See [https://docs.chef.io/resource\_powershell\_script.html](https://docs.chef.io/resource_powershell_script.html)

---

<div class="post-metadata">

**Author:** ![erbeh77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/erbeh77/32/1508_2.png) [@erbeh77](https://discourse.chef.io/u/erbeh77)\
**Post date:** [July 22, 2016, 9:47pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997/3 "2016-07-22T21:47:45Z")

</div>

Thanks, that is showing how to execute an inline powershell script, I want to execute a remote powershell script ex…c:\temp\hello.ps1

---

<div class="post-metadata">

**Author:** ![Matt\_Wrock](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/matt_wrock/32/3394_2.png) [@Matt\_Wrock](https://discourse.chef.io/u/Matt_Wrock)\
**Post date:** [July 22, 2016, 10:26pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997/4 "2016-07-22T22:26:09Z")

</div>

Hmm. The fact that c:\temp\hello.ps1 is in `c:\` indicates it is local. Are you trying to run a local script on a separate remote node? Or maybe we are just mincing terms and you just want to run a script from disk inside of the chef run. I’ll go with that but correct me if I’m off.

I’d stick with the `powershell_script` resource and do something like:

```auto
powershell_script 'run remote script' do
    code '. c:\temp\hello.ps1'
end

```

This should simply “dot source” the script. If the code in the script is encapsulated inside of functions. Youd want to do:

```auto
powershell_script 'run remote script' do
  code <<-EOH
  . c:\temp\hello.ps1
  Invoke-MyFunction
  EOH
end

```

---

<div class="post-metadata">

**Author:** ![erbeh77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/erbeh77/32/1508_2.png) [@erbeh77](https://discourse.chef.io/u/erbeh77)\
**Post date:** [July 25, 2016, 3:04pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997/6 "2016-07-25T15:04:19Z")

</div>

Yes local script, but the following code doesn’t do any execution:

```
powershell_script 'run remote script' do
    code '. c:\temp\hello.ps1'
end

hello.ps1:
$text = 'Hello World'
$text >> 'file.txt'

```

run that locally and it works, missing something…

---

<div class="post-metadata">

**Author:** ![Matt\_Wrock](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/matt_wrock/32/3394_2.png) [@Matt\_Wrock](https://discourse.chef.io/u/Matt_Wrock)\
**Post date:** [July 25, 2016, 3:12pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997/7 "2016-07-25T15:12:43Z")

</div>

hmmm. instead of `'file.txt'` use an absolute path just to make sure its saving to where you expect.

---

<div class="post-metadata">

**Author:** ![erbeh77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/erbeh77/32/1508_2.png) [@erbeh77](https://discourse.chef.io/u/erbeh77)\
**Post date:** [July 25, 2016, 3:15pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997/8 "2016-07-25T15:15:18Z")

</div>

that worked!!! Thank you:smiley:

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 4, 2017, 12:36pm UTC](https://discourse.chef.io/t/how-to-execute-a-powershell-script/8997/9 "2017-09-04T12:36:00Z")

</div>

i want to pass a variable $instance = #{node[‘t1’][‘message’]}.  
i want it to pass through code property of powershell\_script resource

can i do like this ???

powershell\_script ‘run remote script’ do  
code '. C:\t1\files\default\shell\_cmd.ps1 “$instance”'  
end
