Wednesday, January 25, 2012

Script to reset “Hidden files and folders” to normal using PowerShell


Hi,

yesterday one of my users laptop get infected with virus. After removal of virus i saw that “Virus” hide every file and folder in the laptop.  It was quite a pain to “Choose file and folder” right click on it, choose properties and un-tick the hidden option.

Then i decide to write a script which can do this for me.

Download it from here : http://gallery.technet.microsoft.com/scriptcenter/Reset-Hidden-files-and-27d70766
so here is the script:
## Change "D:\P-temp" with your folder path
$Filepath = Get-ChildItem -Recurse -path "D:\P-Temp\" -Force 
$HSfiles = $Filepath | where { $_.Attributes -match "Hidden"}
$HSfiles
foreach ( $Object in $HSfiles ) {
       $Object.Attributes = "Archive"
       }
### End of script #####
This script search for “Hidden file and folder” in the path you defined in $Filepath and the it sets the “Archive” attribute on every file and folder it found.

Screen Shots:

You can see that the i highlighted the “hidden” files and folder below.

25-01-2012 17-05-46

we run the Script  and it done something ;-)

25-01-2012 18-15-42

You can see that , all hidden folder are back to normal. :)

25-01-2012 17-06-33

Download it from here : http://gallery.technet.microsoft.com/scriptcenter/Reset-Hidden-files-and-27d70766

Thanks for viewing .
Aman Dhally
messenger_freak_adp1 (8)

How to find Hidden files and Folders using PowerShell.

 

Hi,

In Old days when we need to saw the hidden files we used Dir /a command to view them. If you don't know let me show you the example

first we run a simple Dir *. command to show the us the list of folders are it is showing only 11 Directories.

Secondly we run the Dir *. /a command and it showing us 18 Directories , which means we have 7 hidden directories.

25-01-2012 17-20-45

how we can access the same information using PowerShell? it’s very easy just use the Get-ChildItem Cmdlet.

first Run Get-ChildItem only and we can see that it is showing  only few directories.

25-01-2012 17-26-59

OK Now run the Get-ChildItem with –Force Switch.

and you can see now we have more directories listed here. You can notice in the front of some directories the Mode is “h” {green arrow} ,which means the attribute for these directories is set to hidden.

25-01-2012 17-31-48 

or the other way to see the the Hidden files are folder in current folder is to run this command line.

Get-ChildItem  -Force | where { $_.Attributes -match "Hidden"}

this will show you only “Hidden” files and folders.

25-01-2012 17-41-00 

Thanks for reading.,..

Aman Dhally

messenger_freak_adp1 (30)

Part-3: Text Manipulation in PowerShell using .StartsWith() and EndsWith() methods

 

Part-1: http://newdelhipowershellusergroup.blogspot.com/2012/01/part-1-text-manipulation-in-powershell.html | using Trim(),TrimEnd(),TrimStart()

Part-2: http://newdelhipowershellusergroup.blogspot.com/2012/01/part-2-text-manipulation-in-powershell.html | ToUpper(),ToLower()

Hi,

Today we are going to use .StartsWith() and .EndsWith() methods to play with our text.

we are using the same text which we used in previous posts and saving the text in a variable $text

$text = "The Quick brown fox jumps over the lazy dog"

24-01-2012 15-14-58 

Lets start.

.StartsWith()

This test that check that the string start with the character which we specified in .StartWith() and it give result is Boolean, the result is either “True” or “false”

lets check if our text start with “The Quick”

$text.StartsWith("The Quick")

the result is “True” , which means yes our text is start with “The Quick”

24-01-2012 15-21-40

NOTE: By default the the text which we specified in  StartsWith() and EndsWith() are case sensitive. Not getting my point ? let me show you.

this is our default text. “T” in The and “Q” in Quick is uppercase.

24-01-2012 15-14-58 

this time i type all lower case character in .StartsWith() method and lets see the result

$text.StartsWith("the quick")

and this time result is false. You need take care of it if you are going to use these methods in your scripts.

24-01-2012 15-30-58

lets compare.. I hope everything is clear now.

24-01-2012 15-32-45

How to fix it ???

to make these methods make case-insensitive add ,1 after the text. This ,1 is in igonecase  switch which is Boolean, so 1 means is case-insensitive is ON

$text.StartsWith("the quick",1)

Bingo !!! now our condition is true.. :)

24-01-2012 15-43-47 

Lets See the comparison chart. The result are in the front of you…

24-01-2012 15-45-23 

.EndsWith()

lets check if out text ends with “lazy dog”

$text.EndsWith("lazy dog")

The result is “True” which means our text ends with “lazy dog”

24-01-2012 15-48-59

as i mentioned before that the text in these Methods take as case sensitive. in our current string “lazy dog” is in lower case let try to search with “Lazy Dog”

$text.EndsWith("Lazy Dog")

as expected the result is “False”

24-01-2012 15-57-30

lets make it case-insensitive by ON the IgnoreCase switch

$text.EndsWith("Lazy Dog",1)

Bingoo !!! its true now ….

24-01-2012 16-09-31

want to see the comparison? I bet you want…

In the screenshot you can see the original text and the methods with ignorecase and without ignorecase switch

24-01-2012 16-11-05

Thanks

Aman Dhally

Aman Dhally

Tuesday, January 24, 2012

Part-2: Text Manipulation in PowerShell using .ToLower and .ToUpper method

 

Part-1: http://newdelhipowershellusergroup.blogspot.com/2012/01/part-1-text-manipulation-in-powershell.html

Hi,

In part-1 of text manipulation we saw how to use .Trim(),.TrimStart() and TrimEnd() method. Today we will see how to use .ToLower and ToUpper() Method.

Lets start

we are using the same text which we used in part-1 and saving the text in a variable $text

$text = "The Quick brown fox jumps over the lazy dog"

24-01-2012 12-42-07

.ToLower()

When we use ToLower() method it convert the whole string in lowercase.

Check first what $text contains.  and it contains the text which we provide to variable $text

24-01-2012 12-44-23

When we run the $text.ToLower() it converted the whole string in to lower case.

24-01-2012 12-46-13 

Let’s compare it with original one. You can see it converted the whole string to lower case {actually “T” in “The” and “Q” in “Quick” is uppercase in original text}

24-01-2012 12-49-00

ToUpper()

The ToUpper() method convert the whole string in to uppercase.

$text.ToUpper()

You can see that the whole string in converted to UPPER CASE :)

24-01-2012 12-55-24

let compare it with our original text.

Happy????? :)

24-01-2012 12-57-17

I hope it helps someone :)

Thanks

Aman Dhally

Monday, January 23, 2012

Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods


Hi,

Did you ever tried to use .Trim() method on a string and can’t get it working? if yes ? Then welcome to the novice group. and if no? man you are genius.

Kidding :)

i was looking for some example for it i found few but not with screenshots :) .. then i decide to write a article on it … 

So lets start.

I am using the text “The Quick brown fox jumps over the lazy dog” and put this text in to a variable $text
$text = "    The Quick brown fox jumps over the lazy dog.     "

You can see that i have a space in the the beginning of the text and the end of the text.



23-01-2012 16-26-58

Lets remove these spaces to remove these spaces we are going to use trim() method.
Trim()

The Trim() remove the black characters to the right and left 

$text.Trim()

You can see that white space is removed from beginning of “the quick brown”

23-01-2012 16-35-10

TrimStart()

The TrimStart() remove all the chracters from the start of the string.






If you want to remove any from the beginning of the text than we need to use TrimStart() method. Lets remove “The QUick” from the text.

$text.TrimStart("The Quick")

Now you can see in the output “The QUick” is removed from output.



23-01-2012 16-40-41

not lets trim the end of the string using TrimEnd() method

TrimEnd()

The TrimEnd() remove are the characters from the string.







$text.TrimEnd("the lazy dog.     ")

You can see that now “the lazy dog.” is removed from the output.


23-01-2012 17-37-57

I hope this helps someone…


Thanks
Aman Dhally

Friday, January 20, 2012

Uninstall Software using PowerShell

 

Hi,

today i was trying to uninstall the the software using PowerShell.

i See few blog posts about uninstalling software using PowerShell they re recommending WMI to uninstall software using the .Uninstalled() method. But i think this method is not right because it don’t un-installed the software properly. Sometime uninstalling software using these method cause system/laptop to crash.

In my view, un-installation should be done by proper way.

Lets un-install winzip in a proper way. You can also script it.

we are going to use WMI but in another way.

Lets Start

lets see installed software first using the below command.

Get-WmiObject -Class win32_product

it is showing the list of all installed software.

 20-01-2012 17-24-32

Now see if we have winzip installed. we are piped the previous command to Where-Object and searching for name which contain winzip in it.

Get-WmiObject -Class win32_product | where { $_.Name -like "*Winzip*"}

Yes we have WinZip installed

20-01-2012 17-27-40 

Lets pipe the above command to Format-List to that we can see all the members of the Winzip.

Get-WmiObject -Class win32_product | where { $_.Name -like "*Winzip*"} | format-List *

This will give us  all the member properties of Winzip. The property in which we are interested is “LocalPackage”

20-01-2012 17-31-01

and you can see that install LocalPackage contains a path to a .msi executable file. and we can uninstall .msi files uisng MSIEXEC. lets check the which command line switches MSIEXEC support.

Simple run MSIEXEC and it will show you all the switches msiexec support.  The switch which we are going to use is /x and /passive

20-01-2012 17-38-01

lets combine the all commands together. I put the first command in to $Wzip variable.

  1. $Wzip = Get-WmiObject -Class win32_product | where { $_.Name -like "*Winzip*"}
  2. $Wzip.localPackage

Now try to run $Wzip.localPackage and this will show us the WinZip install source

20-01-2012 17-43-54 

Run the below command to uninstall the Winzip.

msiexec /x  $wzip.localpackage /passive

this will start un-installation of Winzip and will show only the Progress bar only {because we are using msiexex’s /passive switch”

Here it start uninstallation .

20-01-2012 18-05-01 

20-01-2012 18-05-38 

20-01-2012 18-05-44 

20-01-2012 18-06-37 

All done !!!! lets check Programs and features in Control Panel and winzip is not here :)

20-01-2012 18-11-05 

 

You are save all this in script and use it to uninstall winzip or any other software using powershell.

Hope it helps someone :)

 

Thanks

Aman Dhally

 

How to use –Exclude switch in Remove-Item cmdlet in PowerShell


Hi,
today i was trying to remove some junk folders and files in a  specific folder. But i don’t want to remove all of file and folder , i want to keep few folders. Then i think that i should use the –Exclude switch with Remove-Item Cmdlet.
I tried to use –Exclude switch but somehow i failed to get it working because i don't know the exact patter used by –Exclude switch. After few minutes of testing i get it done and i thought i should share this tip with you.
  1. $Lenovo = "D:\P-Temp\Lenovo\*"
  2. Remove-Item -Recurse  -Path $Lenovo -Exclude system,temp,updates.ser,"*.xml"   -VerboseForce
In $Lenovo variable i specified the folder path in which I want to remove the items
I want to Exclude folder name “System”,”temp” and file name “updates.ser” and all .XML Files
in -Exclude switch give the folder name which you don’t want to delete,  no need to put the in a double quotes “” in folder name . You can provide multiple folder name separated by comma.
in pattern matching make sure you put the wild cards in double quotes “”.

Lets Test it !!
see we have System,Temp, few XML and one Updates.Ser file which we exclude to deleted. lets run the script now and see which file is getting deleted.
20-01-2012 12-48-32
after running the script .
All folders are stay intact which we exclude in the Remove-Item cmdlet.
20-01-2012 12-52-56
I hope that i helps someone .
thanks
aman dhally


Monday, January 16, 2012

Alias and Powershell profiles

 

Hi All

I love “RUN” box.  We can open run box “Windows Key + R” and type the Excel, Powerpoint, and click on OK and its open these application which are in the System path.

1

But if you try to run “Excel” or “Powerpoint” etc in PowerShell console they wont work.

13

Everyday i open firefox, Internet Explorer, Ms Word, Ms Excel, Ms PowerPoint from “RUN” box. but today i thought that these application must me run from my powershell console.

The benefit of this is that i don't need to open “RUN” box every time before running any command.

Windows PowerShell Profile

First we need to create a windows powershell profile. It is a simple .ps1 file which reside on your Document folder and it run before opening PowerShell console.

Now open PowerShell Console and in Console type notepad $profile

If you don’t have profile created before it ask you to create a new file. Click on Yes.

2

First i want that whenever I open “PowerShell” console it should run in “D:\PowerShell”, to achieve this we need to use Set-Location cmdlet.

Set-location D:\PowerShell

3

Now save the file and close it. and lets open PowerShell Console again.

Cool!!! now the default path of PowerShell console is set to “D:\PowerShell”

4

Aliases

Our Next Step is to setup alias. to achieve this we are using  Set-Alias .

The Syntax to setup alias is Set-Alias <Name> <Path of file >

We are setting up alias for Word,Excel,Powerpnt,Outlook,Firefox,Internet,Explorer,Chrome,

and the command should be : 

Note: I am using Office 2010 and may be your office path may be different.

 

Set-Alias word        'C:\Program Files\Microsoft Office\Office14\WINWORD.EXE'

Set-Alias powerpnt   'C:\Program Files\Microsoft Office\Office14\POWERPNT.EXE'

Set-Alias excel      'C:\Program Files\Microsoft Office\Office14\EXCEL.EXE'

Set-Alias outlook    'C:\Program Files\Microsoft Office\Office14\OUTLOOK.EXE'

Set-Alias firefox    'C:\Program Files\Mozilla Firefox\firefox.exe'

Set-Alias Iexplore   'C:\Program Files\Internet Explorer\iexplore.exe'

Set-Alias Chrome     'C:\Users\aman.dhally\AppData\Local\Google\Chrome\Application\chrome.exe'

now open our powershell profile again and paste the above command.

5

save the file and close it. and also close the PowerShell console and open it again.

Lets test it now

- Excel => Working

6

- Word => Working

7

- PowerPoint => Working

8

- Outlook => Working

9

Our all Ms Office applications are working .. lets test browsers now.

- Chrome => Working

10

- Firefox => Working

11

- Internet Explorer => Working

12

All is working .. u can add more alias if you want.

Thanks

Aman Dhally