# Function in a module needs to run elevated

**URL:** <https://forums.powershell.org/t/function-in-a-module-needs-to-run-elevated/26245>\
**Category:** PowerShell Help\
**Created:** [November 24, 2025, 8:31pm UTC](https://forums.powershell.org/t/function-in-a-module-needs-to-run-elevated/26245 "2025-11-24T20:31:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wshazlett](https://avatars.discourse-cdn.com/v4/letter/w/5f8ce5/32.png) [@wshazlett](https://forums.powershell.org/u/wshazlett)\
**Post date:** [November 24, 2025, 8:31pm UTC](https://forums.powershell.org/t/function-in-a-module-needs-to-run-elevated/26245/1 "2025-11-24T20:31:31Z")

</div>

I’m new to scripting in general and have a question. I need to call a function in an in-house module that only needs to run elevated the first time. It creates an EventLogSource. Where do I need to put my elevation script block? In the script calling the function or the module itself? Here’s an example of what needs to run elevated. The function that is called runs this snippet is in a module that was built in-house.

try {  
if (-not (Test-Path -Path $logSourcePath)) {

# if not already registered as an event log source

New-EventLog -LogName Application -Source $LogSourceName

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [November 25, 2025, 2:52pm UTC](https://forums.powershell.org/t/function-in-a-module-needs-to-run-elevated/26245/2 "2025-11-25T14:52:16Z")

</div>

It sounds like you always need elevation to run new-eventlog if the path does not exist. Something like this should work. It will prompt UAC if it needs to create the log path.

```auto
if (-not (Test-Path -Path $logSourcePath)) {
    Start-Process -FilePath "pwsh" -ArgumentList "-NoProfile -Command New-EventLog -LogName Application -Source '$LogSourceName'" -Verb RunAs -Wait
}

```

---

<div class="post-metadata">

**Author:** ![wshazlett](https://avatars.discourse-cdn.com/v4/letter/w/5f8ce5/32.png) [@wshazlett](https://forums.powershell.org/u/wshazlett)\
**Post date:** [November 25, 2025, 3:07pm UTC](https://forums.powershell.org/t/function-in-a-module-needs-to-run-elevated/26245/3 "2025-11-25T15:07:51Z")

</div>

> [@psdarwin](#):
>
> `Start-Process -FilePath "pwsh" -ArgumentList "-NoProfile -Command New-EventLog -LogName Application -Source '$LogSourceName'" -Verb RunAs -Wait`

Thank you SO much. That worked in my first couple of simple tests. Now on to a more realistic test.

Again, you’re a life saver.

---

<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:** [December 25, 2025, 3:07pm UTC](https://forums.powershell.org/t/function-in-a-module-needs-to-run-elevated/26245/4 "2025-12-25T15:07:54Z")

</div>

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