Powershell Scripts & Stuff

Powershell Scripts & Stuff

This is some of the powershell scripts that I have used to keep our computer systems running smoothly. I’ve set them as scheduled tasks.

Now before I go into the powershell scripts I’m going to mention how to set them up as a scheduled task. I’m not going to bring up the whole process to coming up with a scheduled task. At least not at the moment. For now google how to set up a scheduled task.

For now I’m just going over what the settings need to be. So as a task it needs to open powershell. That means entering this location of the program.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

For the add arguments you need -File and the location of the script. So something like.

-File “C:\Scripts\script.ps1

Then for the start in section I enter the directory of the script so…

C:\Scripts

There is one thing so far I’ve had to do which is change the execution policy on the server that does all the work with the powershell scripts.

Basically to get it to run unsigned scripts. So first off here’s how you do that. Open up powershell as a Administrator and enter one of the two following lines.

If you’re going to create the files yourself by typing this code into powershell and saving it yourself. Then the following command should work.

Set-ExecutionPolicy RemoteSigned

If for some reason that doesn’t work with powershell scripts you’ve downloaded from the net then you may have to use.

Set-ExecutionPolicy Unrestricted

Now for the sake of mentioning it the default is…

Set-ExecutionPolicy Restricted

And if you have the ability to sign your powershell scripts then this would be the command. But honestly if you know how to sign your own powershell scripts I doubt any of this code is helpful to you. In face you can probably tell me if I messed up anything. 🙂

Set-ExecutionPolicy AllSigned

 

Ok Now for the scripts you just have to enter this as a powershell command. Or put it into Powershell ISE and then save it as a file and then you’ll have your script.

    1. Reboot the computer: I know this one is really stupid simple I shouldn’t even bother with it, but I’m going to post it anyway.
      Restart-Computer

      There’s more options you can add, but honestly I don’t need or use it at the moment. So instead I’ll just point to the article right here.

    2. Copy files to a backup and add the date to the file name. As well as remove anything older than 15 days: Can’t have enough backups.
      # The source of the files you want to backup. This will back up the entire directory.
      $source = "C:\source_of_files_to_backup\"
      # The place the files will go to. I usually throw them into a dropbox folder.
      $backuppath = "C:\place_of_backups\"
      $limit = (Get-Date).AddDays(-15)
      Get-ChildItem -Path $source | ForEach-Object {
      Copy-Item -Path $_.FullName -Destination $backuppath$((Get-Date).ToString("MMddyyyy"),$_.BaseName,$_.Extension)
      }
      # Delete files older than the $limit.
      Get-ChildItem -Path $backuppath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
    3. Check to see if service is up. If not bring it up: We have a couple of services for some reason that can be problematic so it’s good to have a script to make sure they are up and running. Mainly sql, but I blame a vendor for this.
      $ServiceName = 'whatever_the_service_name_is'
      $arrService = Get-Service -Name $ServiceName
      while ($arrService.Status -ne 'Running')
      {
          Start-Service $ServiceName
          write-host $arrService.status
          write-host 'Service starting'
          Start-Sleep -seconds 60
          $arrService.Refresh()
          if ($arrService.Status -eq 'Running')
          {
              Write-Host 'Service is now Running'
          }
      }

      And while I mention that sql can be one of our issues I’m going to include a link to some other sql based powershell commands. While I don’t use them yet I think I’ll be looking into this more. Here’s the link.

    4. Moving files – I kind of did this with backing up honestly, but that was designed for backing up and entire directory as well as adding a time stamp. So I’ll shorten it up for you.
      Copy-Item "C:\fileshere\*" "C:\filesthere\"
      

      For a single file it’s pretty much the same thing

      Copy-Item "C:\fileshere\file.txt" "C:\filesthere\file.txt"
      

      There’s a couple other examples at this link.

    5. Deleting old files – I’ve got alot of old files that I no longer need instead of deleting them myself I let the computer do it for me Here’s the code that gets that done.
      $limit = (Get-Date).AddDays(-x)
      
      # Delete files older than the $limit.
      Get-ChildItem -Path "C:\filewithdeleteableitems\" -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

      You just have to change the x to something like 30 days, and chage the folder you want to purge old items from.

That is it for the time being. I will hopefully have more soon as I am converting some old batch files.

Leave a Reply

Your email address will not be published. Required fields are marked *