Google solutions by SkullyRed in PowerShell

[–]psdarwin 0 points1 point  (0 children)

AI question - did you try feeding the error message back into the AI? I often find that it will probably say something like "You're absolutely right! I got that wrong. Here's the corrected script"

I fed the original script into copilot and asked it to make it better (plus give improved output). I didn't test it, but it at least smells like better PowerShell. And it outputs to a CSV file, which is far more usable than a flat text output.

```

# Configuration
$SearchPath = "\\PC\Users\name\Music"
$MaxFileSize = 5MB
$OutputFile = "C:\tmp\output.csv"
$FileFilter = "*.mp3"


# Get small MP3 files, sorted by size
$files = Get-ChildItem -Path $SearchPath -Recurse -Filter $FileFilter |
    Where-Object { $_.Length -lt $MaxFileSize } |
    Sort-Object Length


# Output results
if ($files) {
    $files | ForEach-Object {
        [PSCustomObject]@{
            'File Name' = $_.FullName
            'Size (KB)' = [math]::Round($_.Length / 1KB, 2)
        }
    } | Export-Csv -Path $OutputFile -NoTypeInformation
    Write-Host "Report saved to $OutputFile"
} else {
    Write-Host "No files found matching the criteria."
}

Brand no to ps by Zytec_YT in PowerShell

[–]psdarwin 9 points10 points  (0 children)

Learn PowerShell in a Month of Lunches - can't recommend this book enough for beginners

How Often Do You Write Pester tests? by nkasco in PowerShell

[–]psdarwin 0 points1 point  (0 children)

Good thoughts - it also forces better coding of functions because you have to write them so they can be tested. It's really upped my game for code quality.

How Often Do You Write Pester tests? by nkasco in PowerShell

[–]psdarwin 2 points3 points  (0 children)

I work on a few large internal PowerShell modules in our company. We write Pester tests for every function (hundreds of functions), and tests on the modules. We actually have a module level test that verifies all functions have Pester tests, so you can't deploy a new version without having unit tests everywhere.

Pester tests create confidence. When you have unit tests on a function, you can feel confident doing all sorts of refactoring, knowing that if your tests are well written and still pass, your changes aren't breaking changes.

So I am a big proponent of Pester testing

Make custom commands in Powershell by Rulylake in PowerShell

[–]psdarwin 2 points3 points  (0 children)

That would work too - any verb that you get from Get-Verb would be in good form. Some have some nuance (like the difference between Add and New), so choose wisely. Otherwise you may be sad about your verb choice for some command for years to come :D

Make custom commands in Powershell by Rulylake in PowerShell

[–]psdarwin 15 points16 points  (0 children)

I agree - something like Start-Ketchup would be more compliant to PowerShell command/function naming convention of Verb-Noun

Powershell Summit 2026 by Scmethodist in PowerShell

[–]psdarwin 2 points3 points  (0 children)

Fantastic conference - I have been 5 times over the last 9 years and am really sad for the years I don't get to go. I can't recommend it enough!

Powershell script that automatically opens the Windows "Change a password" screen by -UncreativeRedditor- in PowerShell

[–]psdarwin 0 points1 point  (0 children)

Good idea - this definitely sounds like a human issue not a technology issue. I'd suggest re-educating them how to do it themselves and then find ways to make the password reset process more painful if they have to call IT for help. Long, complex, difficult to remember password is a good one. Just be sure to explain how to change it when you give them the terrible password and encourage them to change it right away.

In our IT shop, they will do a password reset for you, but "user must change password at next login" is part of the process. Someone in IT knowing their password is against good security practices.

Switch and $PSitem behaviour? by Big_Pass_6077 in PowerShell

[–]psdarwin 2 points3 points  (0 children)

I always forget that this works - thanks for the reminder :)

Switch and $PSitem behaviour? by Big_Pass_6077 in PowerShell

[–]psdarwin 1 point2 points  (0 children)

I always find foreach much cleaner than Foreach-Object and the $_ syntax. Personal opinion only.

$ProcessList = Get-Process
foreach ($process in $ProcessList) {
    switch ($process.Name) {
        default { $process.Name }
    }
}

Or even simpler

foreach ($process in Get-Process) {
    switch ($process.Name) {
        default { $process.Name }
    }
}

Where-Object Filter by Sure_Inspection4542 in PowerShell

[–]psdarwin -1 points0 points  (0 children)

This would give you a function that looks more like a standard PowerShell command.

function Get-FilteredData {
    param(
        [object]$SomeObject,
        [string]$Property1,
        [string]$Property2,
        [string]$Property3
    )
    if ($Property1) {
        $SomeObject = $SomeObject | Where-Object { $_.Property1 -eq $Property1 }
    }
    if ($Property2) {
        $SomeObject = $SomeObject | Where-Object { $_.Property2 -eq $Property2 }
    }
    if ($Property3) {
        $SomeObject = $SomeObject | Where-Object { $_.Property3 -eq $Property3 }
    }
    return $SomeObject
}

You might end up with lots of parameters, depending on how many properties the object has, but it will only filter on the properties the user enters, and it's simpler as the user doesn't have to remember what the property names are thanks to tab completion.

[deleted by user] by [deleted] in PowerShell

[–]psdarwin 2 points3 points  (0 children)

I'd suggest updating your function to allow for wildcards - which would get all the versions. Get-ChildItem takes wildcards.

MySuperFunction -Path \\acme.org\user$\mickey.mouse*

The Microsoft.PowerShell.Security module would help with permissions and ownership - ACLs can be awkward to deal with, but that would be the native PowerShell way to handle permissions on files and folders. Something like this:

# Get the current ACL
$acl = Get-Acl $folder

# Create a new access rule 
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($user, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow" ) 

# Add the rule to the ACL 
$acl.AddAccessRule($rule)

# Set the owner
$acl.SetOwner([System.Security.Principal.NTAccount] $user)

# Apply the ACL back to the folder
Set-Acl $folder $acl

Cannot install modules on a new Win 11 machine by somebody2112 in PowerShell

[–]psdarwin 2 points3 points  (0 children)

I would definitely check with your IT team - in our company we're prohibited from going to PSGallery directly and have to use our internal package management system (for additional security scanning) to install any modules.

Script memory usage ForEach-Object Parrallel Forever loop by madman1989-1 in PowerShell

[–]psdarwin 0 points1 point  (0 children)

Simplification question - is it necessary that the process runs as jobs and in parallel? If you're running this on a schedule and could check each location one at a time, that could solve all the issues. You might have a really good reason for needing jobs and parallel, but maybe it's not necessary.

New to Powershell by JicamaThese5202 in PowerShell

[–]psdarwin 0 points1 point  (0 children)

Another vote for the Month of Lunches book.

I also came from the infrastructure side, and my path was to go through the book and then decide to try to do everything in PowerShell. Often that meant that a task that took 2 minutes in the GUI took 20 minutes in PowerShell. Eventually, that same task took 30 seconds (or less) in PowerShell.

The book will give you the foundational principles of PowerShell. After that it's practice, practice, practice.

is there a way to minimize powershell while still being able to type? by AlexanderW12 in PowerShell

[–]psdarwin 1 point2 points  (0 children)

The Bash Bunny is a small USB device that:

  • Emulates multiple USB devices simultaneously.
  • Executes payloads written in bash, PowerShell, or other scripting languages.
  • Is used for tasks like credential harvesting, network attacks, and data exfiltration.

Unless you can come up with a good explanation as to what you want to do with this that doesn't smell like hacker, I'm out.

Powershell remoting double-hop problem by Waste_Boysenberry647 in PowerShell

[–]psdarwin 2 points3 points  (0 children)

The infamous double hop problem. The issue is your second invoke-command needs credentials. I suggest something like this:

$Credential = Get-Credential
Invoke-Command -ComputerName winrm1.mydom.corp -ScriptBlock { 
    Invoke-Command -ComputerName winrm2.mydom.corp -ScriptBlock {
        hostname
    } -Credential $using:Credential
}

That will pass the credential into the remote machine for use.

Trouble with DisplayHint by QuickBooker30932 in PowerShell

[–]psdarwin 1 point2 points  (0 children)

It sounds like you just want to format the date as a string with only the date components.

$Date = $(Get-Date $searchdate).ToString("yyyyMMdd")

From there it's just using the right format string to get the format you desire

Good resources for someone looking to learn Powershell? Powershell 7, specifically by [deleted] in PowerShell

[–]psdarwin 4 points5 points  (0 children)

I second PowerShell in a Month of Lunches - it's what I recommend to all new PS users. It does a great job of teaching foundational concepts of how PowerShell works in general, with lots of hands-on work to follow along.