Showing posts with label Test-Path. Show all posts
Showing posts with label Test-Path. Show all posts

Thursday, May 12, 2016

PowerShell cmdlet of the Day : Test-Path

 

Believe me or not! you are going to use this cmdlet in almost all of your scripts.

By using Test-Path cmdlets, we can find if specific file / folder exists or now. If the file/folder does exists then the output is True, and if they don't then the output is False.

The basic usage of the cmdlet is simple, type the Test-Path cmdlet and in -Path parameter provide the name of file/folder which you want to test.

Test-Path -Path C:\temp\ad.txt

444

In above screenshot, the folder and file both exists, so the output of command in True.

555

In the above screenshot, neither the folder and file exists, so the output of command is false.

Cool! no :)

With Regards.
Aman Dhally
If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel
for PowerShell video tutorials. You can download all of my scripts from “Microsoft
TechNet Gallery”.

 

Monday, March 18, 2013

Powershell Script : Create An Encrypted Folder using Powershell.

Hi,

Windows Encryption is one of the best inbuilt security tool in windows7. My and my friends use windows encrypted file systems (EFS) a lot and may be you are using EFS in your windows environment.

Rather that create a whole DISK, My Documents, Desktop encrypted, I always prefer to create an encrypted folder somewhere on the disk and then put the files which you want to be encrypted in that encrypted folder.

I have written a small Powershell Script which created a encryption enabled folder on your desktop with folder name “Encrypted-Folder”.

You can download the script from this link http://gallery.technet.microsoft.com/scriptcenter/Create-Encrypted-Folder-6f0fe0c9

Script Logic
I have created a small flow charts on how this script works.
Encryption Script Sample
How it works.
  1. This script check for folder name “Encrypted-Folder” on your Desktop.
  2. If the Folder Exists.
    1. then it check if the attributes of the “Encrypted-Folder” is contains encrypted.
      1. If the Folder Attributes on “Encrypted-Folder”  contains “Encrypted”
      2. Then it won’t do anything.
      3. but
      4. If the Folder attributes doesn’t contains “Encrypted-Folder” 
      5. then
      6. it runs “cipher /e” command to make folder encrypted
  3. If the folder Does not exists,
    1. then this create a new folder “Encrypted-Folder”  on your desktop
    2. and encrypt it.
In this script i am using “cipher /e” to enable encryption. I have tested this script on windows7 and windows8 and it is working fine for me.
Screenshots.
18-03-2013 15-55-02
18-03-2013 16-15-19
18-03-2013 16-16-04

You can download the script from this link http://gallery.technet.microsoft.com/scriptcenter/Create-Encrypted-Folder-6f0fe0c9

That’s all for now.
Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007

Friday, August 31, 2012

Setting Microsoft Office 2012 Trust Center's Trusted Location Paths using Powershell.

Hi Everyone.

If you are working in a Telecom Industry, and if your Analysts and Consultants are using in house built templates then i am sure that you are 98% facing the “Trust Centre warning”.

We are using custom in house built Office Templates for Word,Excel, and PowerPoint.  and the problem was whenever we open our in house templates is Office 2012 we were facing the below error.

We can click on it but it is a quite annoying.

31-08-2012 14-47-12

To resolved this error, the solution is to add our custom built templates directory to the  trusted location in trust center manually on every laptop and desktop.

31-08-2012 15-14-10

Did i mentioned MANUALLY???

Naaaaaa !!!!!!! I thought that we can script this.

And to resolve this is wrote a simple script. you can download the script from this link :
http://gallery.technet.microsoft.com/scriptcenter/Setting-office-2010-31d6a5f4

This script is very simple.,

You only need to provide the path of your templates or the location which you want to add as trusted to  $trustlocation variable.
$trustlocation = 'C:\Mylocation\Templates'
and that’s all , when you run the script , the script will create a location registry  named as “Location99” in to the registry.
and the Registry Keys will be look like this .
31-08-2012 15-33-48
and i have added comments in the script for easy understanding,
Functionality.
  1. First script test the path of the “Location99” registry key
  2. and if the Test path fails that will create a New Registry  Folder “Location99” and the the following registry keys
      • AllowSubfolders this is DWORD registry key and the value is 1 to allow subfolders as a trusted location too.
      • Date  = Date of creation
      • Description = Description for location etc.
      • Path  =  Path to the folder
  3. and if folder found it just write a confirmation message in the console that folder do exists.
  4. The script will do the same process for WORD,EXCEL and Powerpoint.
After running the script the location is added to the Word.
31-08-2012 15-15-24

Thanks
Aman Dhally
join aman on facebook Join aman on Linkedin follow aman on Twitter

Monday, February 13, 2012

Automate "Remote Server Administration Tools (RSAT)" Deployment using PowerShell.


HI,

These days I am working on a script which does few “Active directory” task for us. This was seems simple so for but then I have found a major hurdle comes.

Problem:

The script on which I am working will be run on newly formatted system which is having Windows7 installed. The problem was that this script will is using few “Active Directory” cmdlets. And the new installation of windows doesn’t have “RSAT” installed by default. So until or unless I don’t have “ActiveDirectory” module installed till then I can’t use my
 get-ADComputer ; get-ADComputerServiceAccount” cmdlets.

Then I thought there should be way to sort this out. So finally after spending whole day I found a way to do this within my PowerShell Script.

Solution:

The “Active Directory” module only available after installing the “Windows6.1-KB958830-x86-RefreshPkg.msu” package and then enable the “Active Directory Module for windows Powershell” in optional features.

The “Windows6.1-KB958830-x86-RefreshPkg.msu” is a “Microsoft update Standalone Package” and we can install these packages using wusa.exe {Windows Update Standalone installer}.


So after installation of the “Windows6.1-KB958830-x86-RefreshPkg.msu” we need to enable the “Active Directory Module for windows Powershell” in optional features for this we are using dism.exe { Deployment Image Servicing and Management tool}.

The Script:

·         I am placing “Windows6.1-KB958830-x86-RefreshPkg.msu” in a central location so that script the executable from it.
·         Normally I copied all the scripts in “c:\tools” folder and if that folder doesn’t exists it create it for us and write a message on screen that “C:\tools” is created.
o    $tool = test-Path C:\tools
if ($tool -eq $false ) {new-Item -Name "Tools" -Path c:\ -ItemType Directory; write-Host " folder "C:\Tools" created" -ForegroundColor Green }
·         Now it check if “Windows6.1-KB958830-x86-RefreshPkg.msu” file in exists in “C:\tools” folder and if it doesn’t exists it will copy the file from our central share folder  \\soft-server\SoftRes\$hotfix
o    hotfix = "Windows6.1-KB958830-x86-RefreshPkg.msu"
if ($testpath -eq $false) { Copy-Item \\dc3-del\PublicShare\$hotfix c:\tools\
Write-host "Files has Copied Sucessfully, Now I am going to  install the HOTFIX." -ForegroundColor RED
}
·         After copy the file from server it will install the files using wusa.exe
o    & wusa.exe "c:\tools\$hotfix" /quiet | out-null
·         After the installation finishes it will enable the “Active Directory PowerShell Module” feature in “windows Optional feature” using DISM
o    & dism.exe /Online /Enable-Feature /FeatureName:RemoteServerAdministrationTools  /FeatureName:RemoteServerAdministrationTools-Roles /FeatureName:RemoteServerAdministrationTools-Roles-AD  /FeatureName:RemoteServerAdministrationTools-Roles-AD-Powershell | Out-Null


:::::::::::::::::::::::::::::::;; Start of the script ############
$hotfix = "Windows6.1-KB958830-x86-RefreshPkg.msu"
$testpath =  test-path -path "c:\tools\$hotfix"
$tool = test-Path C:\tools
if ($tool -eq $false ) {new-Item -Name "Tools" -Path c:\ -ItemType Directory; write-Host " folder "C:\Tools" created" -ForegroundColor Green } 
if ($testpath -eq $false) { Copy-Item \\soft-server\SoftRes\$hotfix
c:\tools\
Write-host "Files has Copied Sucessfully, Now I am going to  install the HOTFIX." -ForegroundColor RED
}
Write-Host "Installing Remote Server Administration Tools...... " -ForegroundColor Yellow                  
& wusa.exe "c:\tools\$hotfix" /quiet | out-null
Write-Host "HotFix Installed, lets enable the Active Directory Features in "Optional Features"" -ForegroundColor "Green"
& dism.exe /Online /Enable-Feature /FeatureName:RemoteServerAdministrationTools  /FeatureName:RemoteServerAdministrationTools-Roles /FeatureName:RemoteServerAdministrationTools-Roles-AD  /FeatureName:RemoteServerAdministrationTools-Roles-AD-Powershell | Out-Null
Write-Host "Lets Import the Active directory PowerShell Module Now.." -BackgroundColor Green  -ForegroundColor Blue
Import-Module ActiveDirectory
write-Host "Module successfully Imported" -ForegroundColor Red -BackgroundColor White

########## End of the Script ############## 



Screenshots:
13-02-2012 20-41-20 
Note: I tested this script on Windows 7 machine and it works perfectly for me . you can download installation file of "Windows6.1-KB958830-x86-RefreshPkg.msu" from here http://www.microsoft.com/download/en/details.aspx?id=7887. Also this is a simple static script no error checking is enable in it.
 
Thanks for viewing
Aman Dhally
messenger_freak_adp1 (30)