What are the potential pitfalls of using the exec function in PHP to run external scripts like PowerShell?

Using the exec function in PHP to run external scripts like PowerShell can pose security risks if not properly sanitized. To mitigate these risks, it is important to validate and sanitize user input before passing it to the exec function. This can help prevent command injection attacks and ensure that only safe commands are executed.

$command = "powershell.exe -Command \"<your_script_here>\"";
$sanitized_command = escapeshellarg($command);
exec($sanitized_command);