# Including Powershell module\_utils

**URL:** <https://forum.ansible.com/t/including-powershell-module-utils/44152>\
**Category:** Get Help\
**Created:** [July 28, 2025, 2:18pm UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152 "2025-07-28T14:18:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ildjarn](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@ildjarn](https://forum.ansible.com/u/ildjarn)\
**Post date:** [July 28, 2025, 2:18pm UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/1 "2025-07-28T14:18:11Z")

</div>

Hello,

I am trying to move some shared client stuff to a module\_util so I can reuse it in my Powershell scripts. I used [Windows module development walkthrough — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general_windows.html) for documentation.

Situation:

- Local repo (not a Galaxy collection), added as project in AWX
- Folders `plugins/module_utils` and `plugins/modules`

In the `module_utils` directory I have a file `client.psm1` with 1 class, 1 function and an `Export-ModuleMember` for this function.

In the modules directory I have a file `Get-ClientStuff.ps1` with a line `#AnsibleRequires -PowerShell ..module_utils.client`.

When I run the playbook that calls `Get-ClientStuff` I get the error `Could not find collection imported module support code for '..module_utils.client"`.

I tried searching for this error but couldn’t find anything wrong.

Is this because I can’t use classes in module\_utils, or am I doing something else wrong?

Joost

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 28, 2025, 10:26pm UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/2 "2025-07-28T22:26:40Z")

</div>

> - Local repo (not a collection)

The `plugins/` structure is designed for collections and not the [role directory structure](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html#role-directory-structure). The name of module utils in a role directory should be `Ansible.ModuleUtils.Name.ps1` and it would be referenced with `#AnsibleRequires -PowerShell Ansible.ModuleUtils.Name`. I highly recommend using a collection though as that is what most of the documentation is geared towards.

---

<div class="post-metadata">

**Author:** ![ildjarn](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@ildjarn](https://forum.ansible.com/u/ildjarn)\
**Post date:** [July 29, 2025, 8:55am UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/3 "2025-07-29T08:55:40Z")

</div>

I solved the AnsibleRequires error with the following steps:

- Add `./plugins/module_utils` to `module_utils` in `ansible.cfg`.
- Change the AnsibleRequires line to `#AnsibleRequires -PowerShell ansible.module_utils.myapplication`

Next problem: The exported command is not found:

```plaintext
The term 'Connect-MyApplication' is not recognized as the name of a cmdlet,
  function, script file, or operable program. Check the spelling of the name, or
  if a path was included, verify that the path is correct and try again.

```

The contents of myapplication.psm1:

```powershell
class MyApplication {
 /* Class Definition */
}

function Connect-MyApplication {
  param(
    [parameter(Mandatory = $false)]$apihost = 'api.myapplication.com',
    [parameter(Mandatory = $true)]$apiuser,
    [parameter(Mandatory = $true)]$apikey
  )
  return [MyApplication]::New($apihost, $apiuser, $apikey)
}

Export-ModuleMember -Function Connect-MyApplication
```

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 29, 2025, 6:14pm UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/4 "2025-07-29T18:14:11Z")

</div>

The issue now is the name of your module util. If using the role structure module utils need to be in the format `Ansible.ModuleUtils.{name}`. The case doesn’t matter but your `_` in `.module_utils.` is what is causing it to be ignored.

See [ansible/lib/ansible/executor/powershell/module\_manifest.py at ac5eb232e9aa2be82687e016ae05e3883b6a8c33 · ansible/ansible · GitHub](https://github.com/ansible/ansible/blob/ac5eb232e9aa2be82687e016ae05e3883b6a8c33/lib/ansible/executor/powershell/module_manifest.py#L89-L99) for the patterns that are checked. The “builtin” format is what was used for the role directory.

---

<div class="post-metadata">

**Author:** ![ildjarn](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@ildjarn](https://forum.ansible.com/u/ildjarn)\
**Post date:** [July 30, 2025, 7:02am UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/5 "2025-07-30T07:02:57Z")

</div>

Then how can e.g. the microsoft.ad modules work with `#AnsibleRequires -PowerShell ..module_utils._ADObject`? (see [microsoft.ad/plugins/modules/computer.ps1 at main · ansible-collections/microsoft.ad · GitHub](https://github.com/ansible-collections/microsoft.ad/blob/main/plugins/modules/computer.ps1)). As far as I can see, I have the local repo file structure the same as that module:

```auto
\
  plugins\
    module_utils\
      client.psm1
    modules\
      getmyclientdata.ps1
  playbooks\
    getmyclientdata.yml
```

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 30, 2025, 7:51am UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/6 "2025-07-30T07:51:50Z")

</div>

Because that’s a collection and that’s how the collection structure works.

> - Local repo (not a Galaxy collection), added as project in AWX

Unless I’m misreading what you said this meant you weren’t using a collection but trying to use the collection structure in the old role directory structure. The role structure is limited in how it works hence the rigid folder names and what a module util can be called.

---

<div class="post-metadata">

**Author:** ![ildjarn](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@ildjarn](https://forum.ansible.com/u/ildjarn)\
**Post date:** [July 31, 2025, 7:10am UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/7 "2025-07-31T07:10:16Z")

</div>

What I am doing is [Adding modules and plugins locally — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html)  
Apparently in that case, you can’t use AnsibleRequires for powershell modules? It works for Python modules (I can use `from ansible.module_utils.myclient include myclient` which loads the code from `plugins/module_utils/myclient.py`.  
I don’t want to publish my code to Galaxy, so a collection is out of the question.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 31, 2025, 10:25am UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/8 "2025-07-31T10:25:17Z")

</div>

Please have a re read of what I’ve posted. You can use module\_utils for both Python and PowerShell in the old role directory folder structure that you are using. The caveat is that the name of the module util for PowerShell utils need to be `Ansible.ModuleUtils.{name}` so in your example

```
module_utils/Ansible.ModuleUtils.MyApplication.psm1

```

Then in your PowerShell you can do

```powershell
#AnsibleRequires -PowerShell Ansible.ModuleUtils.MyApplication

```

If you want a more relaxed naming standard you **MUST** use a collection. You don’t need to publish a collection it can stay local to your project. If you are wanting to use a collection adjacent to a playbook it can be in the following folder structure

```
main.yml # playbook
collections/
  ansible_collections/
    namespace/
      name/
        plugins/
          module_utils/
            util_name.psm1
          modules/
            module.ps1

```

Change `namespace` and `name` to whatever you want for your collection and you can reference the module in your playbook with `namespace.name.module:`. See [packer-windoze/collections/ansible\_collections/jborean93/windoze/plugins at master · jborean93/packer-windoze · GitHub](https://github.com/jborean93/packer-windoze/tree/master/collections/ansible_collections/jborean93/windoze/plugins) as an example of how a collection can be included adjacent to a playbook.

---

<div class="post-metadata">

**Author:** ![ildjarn](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@ildjarn](https://forum.ansible.com/u/ildjarn)\
**Post date:** [July 31, 2025, 1:36pm UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/9 "2025-07-31T13:36:33Z")

</div>

Since my playbooks are spread over multiple directories (per customer, per service), placing the collection at the same level as the playbooks will not work.  
Renaming the module to `Ansible.ModuleUtils.MyApplication.psm1` worked.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [August 3, 2025, 7:29pm UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/10 "2025-08-03T19:29:58Z")

</div>

> Since my playbooks are spread over multiple directories (per customer, per service), placing the collection at the same level as the playbooks will not work.

Won’t you have the same problem with the role structure? Either option allows you to package it up in a way you don’t have to distribute it public. Collections are just more flexible going forward so it is worth trying to use.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 2, 2025, 7:30pm UTC](https://forum.ansible.com/t/including-powershell-module-utils/44152/11 "2025-09-02T19:30:08Z")

</div>

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