Looking for a reliable way to build custom folder names, deterministic system passwords, or compact database IDs?
Standard PowerShell hashes often lack character variety. This code uses deterministic seeding to generate predictable, fixed-length strings that satisfy strict complexity rules. Meaning: using the same name will generate the same output. Change a single character and get a different output.
$string = "YourCustomStringNameHere"$length = 16 # Mustbeatleast5tofitoneofeachcharactertype
# 1. Define the mandatory pools
$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"$lower = "abcdefghijklmnopqrstuvwxyz"$nums = "0123456789"$syms = "_-"$all = $upper + $lower + $nums + $syms
# 2. Compute the raw hash bytes
$sha = [System.Security.Cryptography.SHA256]::Create()
$bytes = $sha.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($string))
# 3. Seed PowerShell's random generator with the hash bytes so it is deterministic
# (The same input string will always produce the same output string)
$seed = [BitConverter]::ToInt32($bytes, 0)
$rand = [System.Random]::new($seed)
# 4. Force at least one character from each required group to guarantee variety
$result = [System.Collections.Generic.List[char]]::new()
$result.Add($upper[$rand.Next(0, $upper.Length)])
$result.Add($lower[$rand.Next(0, $lower.Length)])
$result.Add($nums[$rand.Next(0, $nums.Length)])
$result.Add($syms[$rand.Next(0, $syms.Length)])
# 5. Fill the rest of the requested length from the combined pool
while ($result.Count-lt$length) {
$result.Add($all[$rand.Next(0, $all.Length)])
}
# 6. Shuffle the final array using the same hash seed so the forced characters aren't always at the front
$shuffled = $result|Sort-Object { $rand.Next() }
-join$shuffled
This technique is considered a Deterministic [Key Derivation Function (KDF)], specifically one that uses a Hash-Based Custom Token Generator.
[Deterministic Token Generation ] "Deterministic" means it is perfectly predictable. If you feed the script the same input string, it will always output the same token. It acts like a random generator, but it lacks true randomness because the SHA-256 hash determines the outcome.
[A Custom Key Derivation Function (KDF)] In cryptography, a KDF takes an initial value (like your string) and derives a completely new, fixed-length key or password from it. Because you forced specific character rules onto the output, it is a customized KDF.
[Pseudorandom String Mapping ] The script uses a pseudorandom number generator (PRNG) seeded by a cryptographic hash. Instead of converting data bit-by-bit (like Base64 or Hex), it uses the hash as a "cheat sheet" to randomly pluck characters out of your custom pool until it hits your exact length.