# Write a function having a -host variable

**URL:** <https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716>\
**Category:** PowerShell Help\
**Created:** [September 11, 2024, 8:34am UTC](https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716 "2024-09-11T08:34:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZaMotH](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@ZaMotH](https://forums.powershell.org/u/ZaMotH)\
**Post date:** [September 11, 2024, 8:34am UTC](https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716/1 "2024-09-11T08:34:33Z")

</div>

about\_Automatic\_Variables will show you a list of variable that we shouldn’t be using. One of those variable is `host`. Hence it is impossible to create a function that has a parameter called `host`. But host is a very common word, and a lot of API are using this name as input …

Question : Is there a way to create a function that can have `host` as input variable ?  
I would like to be able to run this command : `myfunction -host myhostname` without messing up with the Powershell built-in variable `host`. I thought about creating an alias ` [Alias("host")][Parameter(mandatory)][string]$hostX`. But by doing so, which is the most elegant solution I found up to now, I would have both `host` and `hostX` as autocompletion possible switch, which might bring confusion. I thought about hiding it using `DontShow` but this is also hiding the Alias.

Anyone would have a solution that would allow to use a function that has a `-host` switch ?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 11, 2024, 9:28am UTC](https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716/2 "2024-09-11T09:28:25Z")

</div>

I don’t think `-Host` is particularly discoverable. As a PowerShell user, I would be expecting a `-ComputerName` parameter. Rather than `$hostX` why not use `$ComputerName` and alias that?

Even with that solution, I think you’re asking for trouble as you’re inviting users who don’t know that `$host` is an automatic variable to try things that won’t work like:

```PowerShell
$host = 'myHost'
Your-Function -Host $host

```

---

<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:** [September 11, 2024, 9:29am UTC](https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716/3 "2024-09-11T09:29:28Z")

</div>

> [@ZaMotH](#):
>
> I thought about creating an alias ` [Alias("host")][Parameter(mandatory)][string]$hostX`

How about using the usualy used `-ComputerName` instead of `-HostX`?

```auto
[Parameter(Mandatory)]
[Alias("host")]
[string]$Computername

```

---

<div class="post-metadata">

**Author:** ![ZaMotH](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@ZaMotH](https://forums.powershell.org/u/ZaMotH)\
**Post date:** [September 11, 2024, 11:25am UTC](https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716/4 "2024-09-11T11:25:40Z")

</div>

Because I am implementing an Open API module using my converter : [OpenApi-To-PowerShell](https://github.com/tfarray/OpenApi-To-PowerShell). The code is self generated, and I need to write some automation to solve the issue. I thought about having a matching table, but … I would have loved to have the exact same variables as in the Open API configuration file. This brings clarity to whoever whishes to use the converter. Assigning new variable names is bringing confusion, and I wanted to stick as much as possible to the Open API reference.

Actually I am appending an X, exactly like matt suggested. The code is already in github in the last [commit](https://github.com/tfarray/OpenApi-To-PowerShell/commit/926c134f5d6b8c8263817444825571192eb602a3). But I would love to have not to do so.

`$host = 'myHost'` does not work. `$host` is a protected variable you can not modify.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 11, 2024, 12:36pm UTC](https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716/5 "2024-09-11T12:36:34Z")

</div>

> [@ZaMotH](#):
>
> Actually I am appending an X, exactly like matt suggested. The code is already in github in the last [commit](https://github.com/tfarray/OpenApi-To-PowerShell/commit/926c134f5d6b8c8263817444825571192eb602a3). But I would love to have not to do so.

Please read my comment carefully. That’s not what I suggested, I suggested you use `$ComputerName`.

> [@ZaMotH](#):
>
> `$host = 'myHost'` does not work. `$host` is a protected variable you can not modify.

Correct. Again, please read my comment carefully, I’m giving you an example of why I think using `$Host`, even as an alias, is a bad idea.

---

<div class="post-metadata">

**Author:** ![ZaMotH](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@ZaMotH](https://forums.powershell.org/u/ZaMotH)\
**Post date:** [September 11, 2024, 12:46pm UTC](https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716/6 "2024-09-11T12:46:11Z")

</div>

you are correct, my bad 🙂 Multitasking is not always the best thing to do when answering

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 11, 2024, 12:46pm UTC](https://forums.powershell.org/t/write-a-function-having-a-host-variable/24716/7 "2024-10-11T12:46:14Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
