# Simple powershell command to execute

**URL:** <https://forums.powershell.org/t/simple-powershell-command-to-execute/16749>\
**Category:** PowerShell Help\
**Created:** [June 6, 2021, 12:41pm UTC](https://forums.powershell.org/t/simple-powershell-command-to-execute/16749 "2021-06-06T12:41:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MussonKing](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mussonking/32/1384_2.png) [@MussonKing](https://forums.powershell.org/u/MussonKing)\
**Post date:** [June 6, 2021, 12:41pm UTC](https://forums.powershell.org/t/simple-powershell-command-to-execute/16749/1 "2021-06-06T12:41:56Z")

</div>

Hi,  
so I have a very simple power shell command that i want to run automaticly. It works great when doing it in a .bat file, but i would really like to have it under PowerShell

So basicly, what i do without a script to make it work is;

- going in the folder where i want to run the powershell
- right click and open powershell
- type in powershell: python manager.py start
- type in powershell: python manager.py view

And that’s it!

$path = Get-item -Path C:\Users\Chia\Desktop\Swar  
Start-Sleep -Seconds 20  
&$path python manager.py start  
&$path python manager.py view

Thanks for your help 🙂

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 6, 2021, 2:56pm UTC](https://forums.powershell.org/t/simple-powershell-command-to-execute/16749/2 "2021-06-06T14:56:21Z")

</div>

MussonKing,  
Welcome to the forum.

> [@MussonKing](#):
>
> t works great when doing it in a .bat file, but i would really like to have it under PowerShell

Even whem I’m a big fan of PowerShell … you know you can even run CMD with a BAT file from PowerShell, don’t you? 😉

Regardless of that: When you post code or error messages or console output or sample data, please format it as code using the “_preformatted text_” button ( **\</\>** ).  
Thanks in advance.

If you have a given path you can save it to a variable with a simple assignment. You don’t need Get-Item in this case.

```auto
$path = 'C:\Users\Chia\Desktop\Swar'

```

Whatfor do you use `Start-Sleep`? I think you don’t need this.

To combine two elements of a path to a comlete path to be able to use it for a call you can use `Join-Path`. This way you don’t even need to take care about the backslashes.

> **[Join-Path (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/join-path?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The Join-Path cmdlet combines a path and child-path into a single path. The provider supplies the path delimiters.

It could look like this:

```auto
$PythonExecutable = Join-Path -Path $Path -ChildPath 'python.exe'

```

In your case I’d simply change the current working directory to the desired path. You can use either `Set-Location` or `Push-Location`. Push-Location has the advantage that you can come back to the former working directory by calling `Pop-Location`.

> **[Set-Location (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The Set-Location cmdlet sets the working location to a specified location. That location could be a directory, a subdirectory, a registry location, or any provider path. PowerShell 6.2 added support for - and + as a values for the Path parameter....

> **[Push-Location (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/push-location?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The Push-Location cmdlet adds ("pushes") the current location onto a location stack. If you specify a path, Push-Location pushes the current location onto a location stack and then changes the current location to the location specified by the path....

> **[Pop-Location (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/pop-location?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The Pop-Location cmdlet changes the current location to the location most recently pushed onto the stack by using the Push-Location cmdlet. You can pop a location from the default stack or from a stack that you create by using a Push-Location...

You should always read the complete help for the cmdlets you’re about to use including the examples to learn how to use them.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:24pm UTC](https://forums.powershell.org/t/simple-powershell-command-to-execute/16749/3 "2024-05-16T20:24:19Z")

</div>

Closed on May 16, 2024, 8:24pm UTC


