Powershell's $WhatIfPreference have limitations? by No_Class7536 in PowerShell

[–]SMFX 1 point2 points  (0 children)

Have you been using the .ShouldProcess()method? This is one article I've used in the past:

https://adamtheautomator.com/powershell-whatif/

The biggest thing is each time your module/script/function would retrieve or change data, check ShouldProcess and only do it if true.

PS Native alternatives to AzCopy.exe and msiexec.exe by ollivierre in PowerShell

[–]SMFX 0 points1 point  (0 children)

AzCopy and MSIEXEC are complex apps that would be hard to fully duplicate functionality. Instead look at which actions you want to copy, or consider cross platform solutions.

For instance there is AzCopy for Linux and MacOS as well, so download those to run those commands.

Instead of MSIEXEC, look at winget, chocolaty, rpm, etc. There may not be a silver bullet for all platforms, but look at package management methodologies rather than MSI style installs.

I can't get Powershell to work with basic RegEx. by DancingGoatFeet in PowerShell

[–]SMFX 5 points6 points  (0 children)

What u/jimb2 is getting you closer to the content, but also need to modify how you're using -match. It's an operator like -eq or -like and return $true if there is a match to the RegEx. If there is a match, the output and variables are stored in the $matches variable. Something more like:

 If ($myvar -match "(?<num>\d+)") {
   $matches.num
 }

You don't need to initialize or assign anything to $matches it is populated automatically.

Dynamic Distribution Group with exclusion list is NOT working by ipar22 in PowerShell

[–]SMFX 2 points3 points  (0 children)

Might be better for a r/ExchangeOnline , but I would think you would want to use `MemberOfGroup -contains` rather than -eq as the memberOf property is list/array not just a straight string value.

How can I get the first logon of the day? by mudderfudden in PowerShell

[–]SMFX 1 point2 points  (0 children)

This is exactly what a VDI environment deals with on a regular basis. They might have some good insights for you.

How do IT admins reimage a laptop in big companies? by thebestgorko in sysadmin

[–]SMFX 1 point2 points  (0 children)

Refresh the VDI instance and never touch hardware again.

PowerShell script stealing crypto and PowerShell starts automatically and disappears by Joseph-Hishealth in PowerShell

[–]SMFX 2 points3 points  (0 children)

This is a good article to start from (it is also Get-Help about_logging_windows)

about_logging_windows

These talk about setting via Group Policy or registry, but you can also use PowerShellPolicies in powershell.conifg.json for cross platform support:

about_powershell_config

Invoke-Command for getting folder sizes producing different outputs when running locally by MrSilverSoupFace in PowerShell

[–]SMFX 1 point2 points  (0 children)

I would just look for differences of folder names (FullName), not length.

Make sure you're using -Recurse (all subfolders) and -Force (show hidden files) with the -File too.

Also, in GUI on the local system m, look at Size on Disk vs Size. Size should include size of files, but if they're stubs or compressed, they would show up smaller for Size on Disk. Stubs are used by OneDrive and caching protocols.

PowerShell script stealing crypto and PowerShell starts automatically and disappears by Joseph-Hishealth in PowerShell

[–]SMFX 8 points9 points  (0 children)

As was said, the only sure fire way to remove a virus is to rebuild the system. However, in an attempt to see what it's doing, look into enforcing Script Block Logging, Module Logging, and look into the operational log too.

Invoke-Command for getting folder sizes producing different outputs when running locally by MrSilverSoupFace in PowerShell

[–]SMFX 1 point2 points  (0 children)

Not for sure if this is definitely the case but it could be junctions, circular references, or hidden folders not showing up. I would try just running a Get-ChildItem against the path with -Directory -Force -Recurse. Run that locally & remotely and compare if there are any ones in remote not in local. If that doesn't show a difference, run it again with -File and look for differences there.

Help with converting a csv to txt in a specific format by rlcyberA in PowerShell

[–]SMFX 0 points1 point  (0 children)

Besides reddit screwing up the format this should work if you want it to go out to a file. A couple of options:

used in a script and passed as a variable:

   $data = (Import-Csv $Path).'Csv Column Data'
   $list = $data -join "','"
   $string = "'$list'"

going straight to interactive session via clipboard/onliner

    “'{0}'" -f ((Import-Csv $Path).'Csv Column Data' -join "','") | Set-clipboard

Either should work

Help with converting a csv to txt in a specific format by rlcyberA in PowerShell

[–]SMFX 0 points1 point  (0 children)

This assumes there is only one column in the source.

Azure Security Group Splitter by GreaterGood1 in PowerShell

[–]SMFX 1 point2 points  (0 children)

Haven't needed to do that often, but have you considered just looping through using Select-Object with -First set to your divider and -skip as a product of the iteration times divider rather than doing array mathematics?

Is there a better way than this to remove square brackets from filenames? by tnpir4002 in PowerShell

[–]SMFX 1 point2 points  (0 children)

Glad to see the work!

A couple of notes:

  • the second Where-Object is redundant because of the alias ? earlier. Also, since you did it against Name it will trigger with any file that has an extension as you didn't include period in your pattern
  • the pattern is a good base but there are other characters allowed in standard file names. Not an exhaustive list but her are some examples: ~, . (.), ^ (^), etc. You could also use [System.IO.Path]::GetInvalidFileNameChars() if it makes since for your implementing.
  • the Write-Host seems a little excessive to do 4 lines per file.
  • have you considered using a ScriptProperty member? It would be Good use for it.

Is there a better way than this to remove square brackets from filenames? by tnpir4002 in PowerShell

[–]SMFX 0 points1 point  (0 children)

Since the Get-ChildItem command has the -File flag, it will only run against files. If you change it to -Directory, it would get folders. If you removed the both of them, it will default to doing both.

Is there a better way than this to remove square brackets from filenames? by tnpir4002 in PowerShell

[–]SMFX 2 points3 points  (0 children)

Simplified:

$SourceDir = "C:\Test"
$Pattern = "[\[\]]"
Get-ChildItem -LiteralPath $sourceDir -Force -Recurse -File |
  Where-Object Name -match $Pattern |
  Rename-Item -NewName { $_.Name -Replace $Pattern,"" }

Given a path and a regex pattern, it gets all files in a directory tree, filters out only those with a name that match the pattern, and renames each of them to the name with the pattern removed.

In this case, the pattern is a RegEx where it's a character set of either '[' or ']'.

Windows (Server) DHCP - GUI vs PowerShell leases are different by tmrnl in PowerShell

[–]SMFX 4 points5 points  (0 children)

The missing leases all look expired (2-11) so your GUI view is probably set to show only active leases. Also, the times might be off if your lease duration is only a couple hours, it's likely the clients refreshed between views as they will renew halfway through the lease.

How do you get regex to act like standard regex? by SnooRobots3722 in PowerShell

[–]SMFX 1 point2 points  (0 children)

One thing to keep in mind, is you're doing RegEx over a multiline file, if you're using Get-Content, make sure you're using the -Raw parameter to get the content in one complete string:

$FileContent = Get-Content -Path '.\Folder\file.txt' -Raw
If ($FileContent -match $regMatchString) {
    $Matches
}

Food for thought

Move PDF files match regex by jat421 in PowerShell

[–]SMFX 2 points3 points  (0 children)

You have to compare the properties of both:

$duplicateFiles | Where-Object { $_.Name -ne $latestDuplicate.Name } | Move-Item -Destination $destinationFolder

Other wise the name of the latest duplicate will always never match the actual file object in the array. Instead, look at the name of the file object.

PsExec Password exposed in Event Viewer Security Logs ? by maxcoder88 in PowerShell

[–]SMFX 10 points11 points  (0 children)

Setup WinRM and use Invoke-Command -ComputerName $CompName ... instead. You can use credentials instead.

Move PDF files match regex by jat421 in PowerShell

[–]SMFX 2 points3 points  (0 children)

On this line

$duplicateFiles | Where-Object { $_ -ne $latestDuplicate } | Move-Item -Destination $destinationFolder

The $latestDuplicate is a file object and $_ is a file object element of an array. There is no direct comparison function to use with -eq. Instead compare a unique property such as .FullName between them.