Skip to main content
8 votes

Delete folder if it exists in PowerShell

It is better to use pipeline syntax. If there are no files, then nothing will happen: Get-ChildItem $folder -Recurse | Remove-Item
Max's user avatar
  • 81
5 votes
Accepted

Arp-spoof check in PowerShell

split and spread to variables: $IP, $SubnetBits = $Subnet -split '/' your user-function invocation with parentheses is incorrect and works only for single ...
woxxom's user avatar
  • 2,002
5 votes
Accepted

Powershell script to verify Linux-generated md5sum file

here are a few changes i would make. [grin] the ideas ... use Get-Content instead of ReadLines() the speed difference is not ...
Lee_Dailey's user avatar
5 votes

PowerShell - fast remove a directory with 10,000+ files

As pointed out in a comment from @Lee_Daily, reinventing the wheel from an interpreted language like PowerShell will be slower than a native implementation. If your primary concern is speed, use the ...
HackSlash's user avatar
  • 402
5 votes

Any issue with this as a password generator?

Your code looks quite neatly done. I would likely make a few changes: make the number of words to use a parameter [with a default value]. 3 is a good starting point, but folks may want 4 or more ... ...
Lee_Dailey's user avatar
4 votes

Delete folder if it exists in PowerShell

This will delete an empty folder as well and still propagates errors if files cannot be deleted ...
arberg's user avatar
  • 141
4 votes
Accepted

Grouping all mathematical results using a number sequence with basic math operators

About performance (there's a good article to get your started: Slow Code: Top 5 ways to make your Powershell scripts run faster); I apply that advice as follows: Problem #1: Expensive operations ...
JosefZ's user avatar
  • 554
4 votes
Accepted

Reading event logs of several computers

Basics Aliases PowerShell is meant to be both shell and programming language. As such, it has a concept of Aliases. An Alias is another name for a command. An alias can resolve to a cmdlet, a ...
briantist's user avatar
  • 1,809
4 votes
Accepted

Comparing dates with SharePoint PnP PowerShell

I think it's okay, but we can do better. Let's see what Get-Date returns. Run this: > get-date | get-member It shows that <...
Dangph's user avatar
  • 1,713
4 votes
Accepted

Using PowerShell to deploy a staging environment to production

Just some random notes. Your code seems pretty good to me in general. ...
Dangph's user avatar
  • 1,713
4 votes
Accepted

Inserting data into SQL-Server table dynamically

The main problem with this code is that it's not screening column values, unfortuantely buit in mechanisms in Invoke-SQLCMD aren't viable. It allows to use ...
Bohdan Mart's user avatar
4 votes
Accepted

Determine the third octet of an IP address based on user entered site number

Let's start with a table of inputs and outputs, because it may not be exactly the same as what you expect from your script. The first column is the input range, the other column is the transformation ...
Oh My Goodness's user avatar
4 votes
Accepted

Deploy folder to desired path - PowerShell function

The code is very cleanly written, and its easy to see what is going on. The verb you are using for the command is not officially 'supported'. Every command should stick to the official list of verbs, ...
Withholm's user avatar
4 votes
Accepted

Fetch specific data from nested JSON-file, then append it to a CSV-file

Date conversion ...
ojdo's user avatar
  • 430
4 votes
Accepted

Powershell Export function to create environment variables with bash syntax

The PowerShell-idiomatic approach is to: Define a command (function) that adheres to PowerShell's <verb>-<noun> naming convention, using one of the ...
mklement0's user avatar
  • 156
4 votes

Find all unique file owners through Powershell in an NTFS file system

If you only care about the unique file owners (not the count), something like (not fully fleshed out but to give you an idea): ...
Bangaio's user avatar
  • 141
3 votes

Replicate / Sync files from source to destination with powershell only

Frankly, robocopy.exe is such a good tool for this job and has been a part of the base Windows install since Vista/2008 that I would just use that instead of ...
Bacon Bits's user avatar
3 votes
Accepted

Work with configuration files holding encrypted credentials

One of the main problems here I think is that the encrypted version of the password can only be decrypted by the account who ran the unencrypted configuration first, on the same computer. Your ...
briantist's user avatar
  • 1,809
3 votes
Accepted

Powershell Script to remove printers and drivers

Before relying on old VBS scripts, it pays to check what native PowerShell tools you have available: ...
Dangph's user avatar
  • 1,713
3 votes
Accepted

Clearing files from directories

There's an important principal in programming called Don't Repeat Yourself (or DRY). This says that if there is any repeated code, then that is usually a bad thing. So let's identify the non-repeated ...
Dangph's user avatar
  • 1,713
3 votes
Accepted

Powershell script to save user credentials in a xml file

Instead of letting the user (1) choose a folder and (2) choose a file name, you can just present the user with a Save File Dialog. It also warns the user if he tries to overwrite an existing file. If ...
Heinzi's user avatar
  • 723
3 votes

Script to backup data to specific USB Drive

Get-WmiObject has been superceded by Get-CimInstance since PowerShell 3 and removed since PowerShell 6, so you should avoid ...
Gao's user avatar
  • 1,230
3 votes
Accepted

Read SIDs and set ACLs

nope! [grin] this is almost entirely about style, not performance. you will note that most points are about readability ... the following line is quite true ... better readability gives ...
Lee_Dailey's user avatar
3 votes

What's a better way to decrease cardinality when serializing records

You can use XElement. It is especially useful when constructing XML. ...
rokumaru's user avatar
  • 221
3 votes
Accepted

Get username from full name - PowerShell function

i think the comments cover the why of things, but please feel free to ask if you have any questions. [grin] ...
Lee_Dailey's user avatar
3 votes

PowerShell - fast remove a directory with 10,000+ files

The foreach-object is not run in parallel by default. You need specify the -Parallel parameter. And the version of Powershell ...
Enpeng's user avatar
  • 31
3 votes
Accepted

Creating a specific distribution of random numbers in Powershell

Since you tagged your question with Performance, my example is not totally idiomatic PowerShell. You could build a lookup array with each number as many times as ...
swbbl's user avatar
  • 146
3 votes

Disk usage reporting script performing poorly with lots of files

Your primary performance problem is due to this pattern: $Set = 1..10000 $Array = @() foreach ($i in $Set) { $Array += $i } This is an awful pattern whether you're ...
Bacon Bits's user avatar
3 votes
Accepted

Powershell script to check for processes on remote systems and rename/copy files

In a high level language one could write it even more terse and save a bit on repetitive code. Gramatical ... and-then ... and-then .... Collecting ...
Joop Eggen's user avatar
  • 4,636
3 votes
Accepted

Powershell script that directly executes python script

The two things that jumped out at me are the wall of calls to Write-Host that can be cleaned up using Here-Strings, and how you are implementing the ...
Greg Burghardt's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible