Share via

Powershell script needed

Ibrahim AlHusari 191 Reputation points
2024-04-24T16:35:23.0566667+00:00

closed.....

Windows for business | Windows Server | User experience | PowerShell

Answer accepted by question author

Marcin Policht 92,630 Reputation points MVP Volunteer Moderator
2024-04-24T17:20:51.0333333+00:00

Try the following:

# Define variables
$RegistryKeyPath = "HKLM:\SOFTWARE\Test"
$RegistryValueName = "ProcessCount"
$TextFilePath = "C:\Temp\Test.txt"
# Function to check if registry key exists
Function Test-RegistryKey {
    param (
        [string]$Path
    )
    return (Test-Path $Path)
}
# Function to check if text file exists
Function Test-TextFile {
    param (
        [string]$Path
    )
    return (Test-Path $Path)
}
# Function to get process count
Function Get-ProcessCount {
    return (Get-Process).Count
}
# Function to update registry value
Function Update-RegistryValue {
    param (
        [string]$Path,
        [string]$Name,
        [string]$Value
    )
    Set-ItemProperty -Path $Path -Name $Name -Value $Value -Force
}
# Function to write to text file
Function Write-TextFile {
    param (
        [string]$Path,
        [string]$Content
    )
    Add-Content -Path $Path -Value $Content
}
# Main script
if (!(Test-RegistryKey $RegistryKeyPath)) {
    New-Item -Path $RegistryKeyPath -Force | Out-Null
}
if (!(Test-TextFile $TextFilePath)) {
    New-Item -Path $TextFilePath -ItemType File -Force | Out-Null
}
# Loop for one minute
$EndTime = (Get-Date).AddMinutes(1)
while ((Get-Date) -lt $EndTime) {
    # Get current process count
    $ProcessCount = Get-ProcessCount
    
    # Update registry value
    Update-RegistryValue -Path $RegistryKeyPath -Name $RegistryValueName -Value $ProcessCount
    
    # Write to text file
    Write-TextFile -Path $TextFilePath -Content "$((Get-Date).ToString()): $ProcessCount"
    
    # Wait for one second
    Start-Sleep -Seconds 1
}


If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

hth

Marcin

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.